-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFourthHomework.py
More file actions
57 lines (44 loc) · 1.21 KB
/
FourthHomework.py
File metadata and controls
57 lines (44 loc) · 1.21 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
import time
import math
class Tribonachi:
def __init__(self, length):
self.length = length
self.seq = [0, 0, 1]
def __iter__(self):
for i in range(self.length):
if i == 0:
ret = self.seq[0]
elif i == 1:
ret = self.seq[1]
elif i == 2:
ret = self.seq[2]
else:
k = len(self.seq)
self.seq.append(self.seq[k-3] + self.seq[k-1] + self.seq[k-2])
ret = self.seq[k]
yield ret
class Leibniz:
"""If """
def __init__(self, sum):
self.current_sum = 0
self.seq = []
self.res_sum = sum
def __iter__(self):
n = 0
while round(self.current_sum, 2) != self.res_sum:
cur = ((-1)**n)/(2*n+1)
self.current_sum += cur
n += 1
if n == 100:
print("There are impossible to get given number")
break
yield cur
def main():
# tri_iter = Tribonachi(10)
# for num in tri_iter:
# print(num)
leibniz_iter = Leibniz(0.88)
for num in leibniz_iter:
print(num)
if __name__ == '__main__':
main()