Created
June 13, 2015 10:01
-
-
Save letroot/76886c538fed94655195 to your computer and use it in GitHub Desktop.
I wrote a recursive function to compute the square root of a number using the Newton-Raphson method. I am using an inner function, is that a bad thing? How can I use just one function definition?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def r_sqrt(a): | |
a = float(a) | |
if a == 0.0: | |
return 0.0 | |
x = a | |
def sqrt(x): | |
epsilon = 0.000000000000001 | |
y = (x + a/x) / 2 | |
if abs(y - x) < epsilon: | |
return x | |
else: | |
return sqrt(y) | |
return sqrt(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote a recursive function to compute the square root of a number using the Newton-Raphson method. I am using an inner function, is that a bad thing? How can I use just one function definition and still be recursive?