-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathex_07_2.py
More file actions
29 lines (25 loc) · 726 Bytes
/
ex_07_2.py
File metadata and controls
29 lines (25 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/python
#AUTHOR: alexxa
#DATE: 25.12.2013
#SOURCE: Think Python: How to Think Like a Computer Scientist by Allen B. Downey
# http://www.greenteapress.com/thinkpython/html/index.html
#PURPOSE: Chapter 7. Iteration
# Exercise 7.2
# Encapsulate this loop in a function called square_root that
# takes a as a parameter, chooses a reasonable value of x, and
# returns an estimate of the square root of a.
def square_root(a):
x = a
while True:
if x == 0:
return 0
y = (x + a/x) / 2
if y == x:
return y
x = y
print(square_root(0))
print(square_root(1))
print(square_root(2))
print(square_root(9))
print(square_root(225))
#END