fork download
  1. import math
  2.  
  3. goal = 10.0
  4.  
  5. tempGoal = goal
  6.  
  7. rangeLow=0.0
  8. rangeHigh=0.0
  9.  
  10. while tempGoal>0:
  11. lowerRoot = math.sqrt(tempGoal)
  12. print(str(lowerRoot))
  13. if(lowerRoot%1==0):
  14. rangeLow=lowerRoot
  15. rangeHigh=rangeLow+1
  16. break
  17. else:
  18. tempGoal-=1
  19.  
  20. if(rangeLow<=0):
  21. print("there is no solution")
  22. raise SystemExit
  23.  
  24. for x in range(1,100):
  25. mid = (rangeHigh-rangeLow)/2.0
  26. root = rangeLow+mid
  27. root1 = root*root
  28.  
  29. print("goal: "+str(goal))
  30. print("rangeHigh: "+str(rangeHigh))
  31. print("rangeLow: "+str(rangeLow))
  32. print("mid: "+str(mid))
  33. print(str(root)+" * "+str(root)+" = "+str(root1))
  34.  
  35. if abs(root1 - goal) <= 0.00000000000001:
  36. print("goal is reached in "+str(x)+" iterations")
  37. break
  38. elif root1>goal:
  39. rangeHigh-=mid
  40. else:
  41. rangeLow+=mid
  42. print("\n")
  43.  
Success #stdin #stdout 0.03s 9984KB
stdin
Standard input is empty
stdout
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