-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_7.py
66 lines (48 loc) · 1.19 KB
/
exercise_7.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
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
from math import *
from exercise_6 import factorial
def hailstone_sequence(n):
count = 1
while n != 1:
print("{}. ".format(count), n)
count += 1
if n % 2 == 0: # n is even
n //= 2
else: # n is odd
n = n * 3 + 1
# hailstone_sequence(73)
def test_square_root(x, a):
while True:
y = (a + x / a) / 2
if y == a:
break
a = y
z = sqrt(x)
print(x, y, z, abs(z-y))
# test_square_root(4, 1)
def eval_loop(exp):
if exp != 'done':
print(eval(exp))
expr = input("Please enter expression for calculation: ")
eval_loop(expr)
else:
print('Hope it helped')
# expr = input("Please enter expression for calculation: ")
# eval_loop(expr)
def estimate_pi():
i = 0
r_const = (2 * sqrt(2)) / 9801
while True:
one_by_pi = r_const * (factorial(4 * i) * (1103 + 26390 * i)) / ((factorial(i) ** 4) * (396 ** (4 * i)))
if one_by_pi < 1e-15:
break
i += 1
return 1/one_by_pi
# print(estimate_pi())
def gcd(a, b):
r = a % b
if r != 0:
x = gcd(b, r)
return x
else:
return b
print(gcd(48, 18))