import math goal = 10.0 tempGoal = goal rangeLow=0.0 rangeHigh=0.0 while tempGoal>0: lowerRoot = math.sqrt(tempGoal) print(str(lowerRoot)) if(lowerRoot%1==0): rangeLow=lowerRoot rangeHigh=rangeLow+1 break else: tempGoal-=1 if(rangeLow<=0): print("there is no solution") raise SystemExit for x in range(1,100): mid = (rangeHigh-rangeLow)/2.0 root = rangeLow+mid root1 = root*root print("goal: "+str(goal)) print("rangeHigh: "+str(rangeHigh)) print("rangeLow: "+str(rangeLow)) print("mid: "+str(mid)) print(str(root)+" * "+str(root)+" = "+str(root1)) if abs(root1 - goal) <= 0.00000000000001: print("goal is reached in "+str(x)+" iterations") break elif root1>goal: rangeHigh-=mid else: rangeLow+=mid print("\n")
Standard input is empty
3.1622776601683795 3.0 goal: 10.0 rangeHigh: 4.0 rangeLow: 3.0 mid: 0.5 3.5 * 3.5 = 12.25 goal: 10.0 rangeHigh: 3.5 rangeLow: 3.0 mid: 0.25 3.25 * 3.25 = 10.5625 goal: 10.0 rangeHigh: 3.25 rangeLow: 3.0 mid: 0.125 3.125 * 3.125 = 9.765625 goal: 10.0 rangeHigh: 3.25 rangeLow: 3.125 mid: 0.0625 3.1875 * 3.1875 = 10.16015625 goal: 10.0 rangeHigh: 3.1875 rangeLow: 3.125 mid: 0.03125 3.15625 * 3.15625 = 9.9619140625 goal: 10.0 rangeHigh: 3.1875 rangeLow: 3.15625 mid: 0.015625 3.171875 * 3.171875 = 10.060791015625 goal: 10.0 rangeHigh: 3.171875 rangeLow: 3.15625 mid: 0.0078125 3.1640625 * 3.1640625 = 10.01129150390625 goal: 10.0 rangeHigh: 3.1640625 rangeLow: 3.15625 mid: 0.00390625 3.16015625 * 3.16015625 = 9.986587524414062 goal: 10.0 rangeHigh: 3.1640625 rangeLow: 3.16015625 mid: 0.001953125 3.162109375 * 3.162109375 = 9.99893569946289 goal: 10.0 rangeHigh: 3.1640625 rangeLow: 3.162109375 mid: 0.0009765625 3.1630859375 * 3.1630859375 = 10.005112648010254 goal: 10.0 rangeHigh: 3.1630859375 rangeLow: 3.162109375 mid: 0.00048828125 3.16259765625 * 3.16259765625 = 10.002023935317993 goal: 10.0 rangeHigh: 3.16259765625 rangeLow: 3.162109375 mid: 0.000244140625 3.162353515625 * 3.162353515625 = 10.000479757785797 goal: 10.0 rangeHigh: 3.162353515625 rangeLow: 3.162109375 mid: 0.0001220703125 3.1622314453125 * 3.1622314453125 = 9.999707713723183 goal: 10.0 rangeHigh: 3.162353515625 rangeLow: 3.1622314453125 mid: 6.103515625e-05 3.16229248046875 * 3.16229248046875 = 10.0000937320292 goal: 10.0 rangeHigh: 3.16229248046875 rangeLow: 3.1622314453125 mid: 3.0517578125e-05 3.162261962890625 * 3.162261962890625 = 9.999900721944869 goal: 10.0 rangeHigh: 3.16229248046875 rangeLow: 3.162261962890625 mid: 1.52587890625e-05 3.1622772216796875 * 3.1622772216796875 = 9.999997226754203 goal: 10.0 rangeHigh: 3.16229248046875 rangeLow: 3.1622772216796875 mid: 7.62939453125e-06 3.1622848510742188 * 3.1622848510742188 = 10.000045479333494 goal: 10.0 rangeHigh: 3.1622848510742188 rangeLow: 3.1622772216796875 mid: 3.814697265625e-06 3.162281036376953 * 3.162281036376953 = 10.000021353029297 goal: 10.0 rangeHigh: 3.162281036376953 rangeLow: 3.1622772216796875 mid: 1.9073486328125e-06 3.1622791290283203 * 3.1622791290283203 = 10.000009289888112 goal: 10.0 rangeHigh: 3.1622791290283203 rangeLow: 3.1622772216796875 mid: 9.5367431640625e-07 3.162278175354004 * 3.162278175354004 = 10.000003258320248 goal: 10.0 rangeHigh: 3.162278175354004 rangeLow: 3.1622772216796875 mid: 4.76837158203125e-07 3.1622776985168457 * 3.1622776985168457 = 10.000000242536998 goal: 10.0 rangeHigh: 3.1622776985168457 rangeLow: 3.1622772216796875 mid: 2.384185791015625e-07 3.1622774600982666 * 3.1622774600982666 = 9.999998734645544 goal: 10.0 rangeHigh: 3.1622776985168457 rangeLow: 3.1622774600982666 mid: 1.1920928955078125e-07 3.162277579307556 * 3.162277579307556 = 9.999999488591257 goal: 10.0 rangeHigh: 3.1622776985168457 rangeLow: 3.162277579307556 mid: 5.960464477539063e-08 3.162277638912201 * 3.162277638912201 = 9.999999865564124 goal: 10.0 rangeHigh: 3.1622776985168457 rangeLow: 3.162277638912201 mid: 2.9802322387695312e-08 3.1622776687145233 * 3.1622776687145233 = 10.00000005405056 goal: 10.0 rangeHigh: 3.1622776687145233 rangeLow: 3.162277638912201 mid: 1.4901161193847656e-08 3.162277653813362 * 3.162277653813362 = 9.999999959807342 goal: 10.0 rangeHigh: 3.1622776687145233 rangeLow: 3.162277653813362 mid: 7.450580596923828e-09 3.1622776612639427 * 3.1622776612639427 = 10.000000006928952 goal: 10.0 rangeHigh: 3.1622776612639427 rangeLow: 3.162277653813362 mid: 3.725290298461914e-09 3.1622776575386524 * 3.1622776575386524 = 9.999999983368147 goal: 10.0 rangeHigh: 3.1622776612639427 rangeLow: 3.1622776575386524 mid: 1.862645149230957e-09 3.1622776594012976 * 3.1622776594012976 = 9.99999999514855 goal: 10.0 rangeHigh: 3.1622776612639427 rangeLow: 3.1622776594012976 mid: 9.313225746154785e-10 3.16227766033262 * 3.16227766033262 = 10.00000000103875 goal: 10.0 rangeHigh: 3.16227766033262 rangeLow: 3.1622776594012976 mid: 4.656612873077393e-10 3.162277659866959 * 3.162277659866959 = 9.99999999809365 goal: 10.0 rangeHigh: 3.16227766033262 rangeLow: 3.162277659866959 mid: 2.3283064365386963e-10 3.1622776600997895 * 3.1622776600997895 = 9.9999999995662 goal: 10.0 rangeHigh: 3.16227766033262 rangeLow: 3.1622776600997895 mid: 1.1641532182693481e-10 3.162277660216205 * 3.162277660216205 = 10.000000000302474 goal: 10.0 rangeHigh: 3.162277660216205 rangeLow: 3.1622776600997895 mid: 5.820766091346741e-11 3.162277660157997 * 3.162277660157997 = 9.999999999934337 goal: 10.0 rangeHigh: 3.162277660216205 rangeLow: 3.162277660157997 mid: 2.9103830456733704e-11 3.162277660187101 * 3.162277660187101 = 10.000000000118407 goal: 10.0 rangeHigh: 3.162277660187101 rangeLow: 3.162277660157997 mid: 1.4551915228366852e-11 3.162277660172549 * 3.162277660172549 = 10.000000000026372 goal: 10.0 rangeHigh: 3.162277660172549 rangeLow: 3.162277660157997 mid: 7.275957614183426e-12 3.162277660165273 * 3.162277660165273 = 9.999999999980355 goal: 10.0 rangeHigh: 3.162277660172549 rangeLow: 3.162277660165273 mid: 3.637978807091713e-12 3.162277660168911 * 3.162277660168911 = 10.000000000003363 goal: 10.0 rangeHigh: 3.162277660168911 rangeLow: 3.162277660165273 mid: 1.8189894035458565e-12 3.162277660167092 * 3.162277660167092 = 9.999999999991859 goal: 10.0 rangeHigh: 3.162277660168911 rangeLow: 3.162277660167092 mid: 9.094947017729282e-13 3.1622776601680016 * 3.1622776601680016 = 9.99999999999761 goal: 10.0 rangeHigh: 3.162277660168911 rangeLow: 3.1622776601680016 mid: 4.547473508864641e-13 3.1622776601684564 * 3.1622776601684564 = 10.000000000000487 goal: 10.0 rangeHigh: 3.1622776601684564 rangeLow: 3.1622776601680016 mid: 2.2737367544323206e-13 3.162277660168229 * 3.162277660168229 = 9.99999999999905 goal: 10.0 rangeHigh: 3.1622776601684564 rangeLow: 3.162277660168229 mid: 1.1368683772161603e-13 3.1622776601683427 * 3.1622776601683427 = 9.999999999999767 goal: 10.0 rangeHigh: 3.1622776601684564 rangeLow: 3.1622776601683427 mid: 5.684341886080802e-14 3.1622776601683995 * 3.1622776601683995 = 10.000000000000128 goal: 10.0 rangeHigh: 3.1622776601683995 rangeLow: 3.1622776601683427 mid: 2.842170943040401e-14 3.162277660168371 * 3.162277660168371 = 9.999999999999948 goal: 10.0 rangeHigh: 3.1622776601683995 rangeLow: 3.162277660168371 mid: 1.4210854715202004e-14 3.1622776601683853 * 3.1622776601683853 = 10.000000000000037 goal: 10.0 rangeHigh: 3.1622776601683853 rangeLow: 3.162277660168371 mid: 7.105427357601002e-15 3.162277660168378 * 3.162277660168378 = 9.999999999999993 goal is reached in 47 iterations