lolamontes69

Python/ Using Newtons Method to guess a square root

May 19th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.43 KB | None | 0 0
  1. def square_root(a):
  2.     if a <= 8:
  3.         x = 3
  4.     else:
  5.         x = float(((a/2)/2)/2)
  6.     epsilon = 0.0000001
  7.     while True:
  8.         y = (x +a/x) / 2.0
  9.         if abs(y-x) < epsilon:
  10.             break
  11.         x = y
  12.     print ""
  13.     print "My guess is that the square root of",a,"is",y
  14. """ Guesses square root of 'a' based upon Newtons method which uses
  15. y = (x +a/x) / 2    
  16. then repeats with x=y until y and x even out
  17. """
Advertisement
Add Comment
Please, Sign In to add comment