fork(1) download
  1. def sqrt(n):
  2. # 3 iterations of newton's method, hard-coded
  3. # normalize
  4.  
  5. # find highest bit
  6. highest = 1
  7. sqrt_highest = 1
  8. while highest < n:
  9. highest <<= 2
  10. sqrt_highest <<= 1
  11.  
  12. n /= highest+0.0
  13.  
  14. result = (n/4) + 1
  15. result = (result/2) + (n/(result*2))
  16. result = (result/2) + (n/(result*2))
  17.  
  18. return result*sqrt_highest
  19.  
  20. print(sqrt(4))
  21. print(sqrt(64))
  22. print(sqrt(10000))
  23. print(sqrt(2))
  24. print(sqrt(10**10))
Success #stdin #stdout 0.02s 9984KB
stdin
Standard input is empty
stdout
2.000609756097561
8.002439024390243
100.27228119483036
1.4218903638151426
100323.51068546745