Repository navigation
Expand file tree
/
Copy pathchapter 7
More file actions
117 lines (98 loc) · 3.92 KB
/
Copy pathchapter 7
File metadata and controls
117 lines (98 loc) · 3.92 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""Exercise 7.1. Copy the loop from Section 7.5 and encapsulate it in a function called mysqrt that
takes a as a parameter, chooses a reasonable value of x, and returns an estimate of the square root of
a.
To test it, write a function named test_square_root that prints a table like this:
a mysqrt(a) math.sqrt(a) diff
- --------- ------------ ----
1.0 1.0 1.0 0.0
2.0 1.41421356237 1.41421356237 2.22044604925e-16
3.0 1.73205080757 1.73205080757 0.0
4.0 2.0 2.0 0.0
5.0 2.2360679775 2.2360679775 0.0
6.0 2.44948974278 2.44948974278 0.0
7.0 2.64575131106 2.64575131106 0.0
8.0 2.82842712475 2.82842712475 4.4408920985e-16
9.0 3.0 3.0 0.0
The first column is a number, a; the second column is the square root of a computed with mysqrt;
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 mysqrt(sqrt_guess, sqrt_number):
while True:
y = (sqrt_guess + sqrt_number/sqrt_guess) / 2
if abs(sqrt_guess-y) <= 0.00001:
# direct float comparisons = bad --> work with epsilon
break
sqrt_guess = y
return sqrt_guess
#The function should return something to be used in a subsequent function
#mysqrt(15, 9)
def test_square_root(amount_of_values, sqrt_guess):
i = 1
print("{:<9}{:<14}{:<14}{:<8}".format("index", "mysqrt", "math.sqrt", "diff"))
print("{:<8} {:<13} {:<13} {:<8}".format("-" * 5, "-" * 6, "-" * 9, "-" * 4))
while i < amount_of_values:
column_1 = i
#index of the columns and number that is the basis of the square root
column_2 = mysqrt(sqrt_guess, i)
# square root of the index using my function
column_3 = sqrt(i)
# square root of the index using pythons math.sqrt function
column_diff = abs(column_2 - column_3)
print("{:<8} {:<13.8f} {:<13.8} {:<8.8}".format(column_1, column_2, column_3, column_diff))
i += 1
test_square_root(8, 8)
"""Exercise 7.2.
The built-in function eval takes a string and evaluates it using the Python interpreter. For example:
>>> eval('1 + 2 * 3')
7
>>> import math
>>> eval('math.sqrt(5)')
2.2360679774997898
>>> eval('type(math.pi)')
<class 'float'>
Write a function called eval_loop that iteratively prompts the user, takes the resulting input and
evaluates it using eval, and prints the result.
It should continue until the user enters 'done', and then return the value of the last expression it
evaluated.
"""
def eval_loop():
while True:
user_input = input(">>> write something! Really! Anything!!! \n")
if type(user_input == str):
user_input = user_input.lower()
if user_input == "done":
print("ok, dann ist es vorbei")
break
else:
print({}.format(user_input))
eval_loop()
"""
Exercise 7.3. The mathematician Srinivasa Ramanujan found an infinite series that can be used to
generate a numerical approximation of 1/π:
See page p.70
Write a function called estimate_pi that uses this formula to compute and return an estimate of
π. It should use a while loop to compute terms of the summation until the last term is smaller than
1e-15 (which is Python notation for 10−15). You can check the result by comparing it to math.pi.
"""
import math
def factorial(x):
if x == 0:
return 1
else:
recurse = factorial(x-1)
result = recurse * x
return result
def estimate_pi():
i = 0
FACTOR = 2 * math.sqrt(2) / 9801
while True:
numerator = factorial(4 * i) * (1103 + 26390 * i)
denominator = (factorial(i)) ** 4 * 396 ** (4 * i)
result = FACTOR * numerator / denominator
i += 1
if abs(result)< 1e-15:
break
return 1 / result
# The implemented Ramanujan Function leads to inverse pi, hence 1/result to get pi
print(estimate_pi())