Skip to content

Instantly share code, notes, and snippets.

@pdparker
Created April 5, 2014 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdparker/9988036 to your computer and use it in GitHub Desktop.
Save pdparker/9988036 to your computer and use it in GitHub Desktop.
Newton square root
def newton_sqrt(x,s=1., kmax=100, tol=1e-8):
"""
Uses the newton method to approximate square roots.
Tolerence set to
"""
for k in range(kmax):
pre_s = s
s = .5 * (s + x/s)
if abs(s - pre_s) < tol:
break
print "Converged after %s iterations" %(k)
print "The square root of %s is %20.15f" %(x,s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment