-
Notifications
You must be signed in to change notification settings - Fork 9
/
ex_07_3.py
39 lines (31 loc) · 1.05 KB
/
ex_07_3.py
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
30
31
32
33
34
35
36
37
38
39
#!/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.
# Write a function named test_square_root that prints a table like this:
# The first column is a number, a; the second column is the square root
# of a computed with the function from Section 7.5; the third column is
# the square root computed by math.sqrt; the fourth column is
# the absolute value of the difference between the two estimates.
from math import sqrt
def test_square_root():
a = 1
while a != 10:
x = a / 2
while True:
if x == 0:
print(a, 'zero')
break
y = (x + a/x) / 2
if y == x:
sqrt_a = sqrt(a)
diff = abs(sqrt_a - y)
print(a, '\t', y, '\t', sqrt_a, '\t', diff)
break
x = y
a += 1
test_square_root()
#END