forked from EasonYi/OnlinePythonTutor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqrt.txt
More file actions
23 lines (16 loc) · 543 Bytes
/
sqrt.txt
File metadata and controls
23 lines (16 loc) · 543 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Calculating square roots by Newton's method, inspired by SICP
# http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-10.html#%_sec_1.1.7
def sqrt(x):
def average(a, b):
return (a + b) / 2.0
def is_good_enough(guess):
return (abs((guess * guess) - x) < 0.001)
def improve(guess):
return average(guess, x / guess)
def sqrt_iter(guess):
if is_good_enough(guess):
return guess
else:
return sqrt_iter(improve(guess))
return sqrt_iter(1.0)
ans = sqrt(9)