-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonaci_example.py
76 lines (59 loc) · 1.61 KB
/
fibonaci_example.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
67
68
69
70
71
72
73
74
75
76
"""five ways to give fibonaci list"""
# 1.iterator
class Fibo:
def __init__(self):
self.count = 0
self.num_pre = 1
self.num_aft = 1
def __iter__(self):
return self
def __next__(self):
if self.count == 1:
internal = self.num_aft
self.num_aft += self.num_pre
self.num_pre = internal
return self.num_aft
else:
self.count += 1
return 1
x = Fibo()
for i in range(10000):
print(next(x))
# 2.generator
def fibonaci(n):
count = 1
a, b = 1, 1
while count <= n:
yield b
a, b = b, a + b
count += 1
g = fibonaci(10000) # Don't forget to initialize
for i in range(10000):
print(next(g))
# 3.recursion (too slow)
def fibonaci2(n):
if n == 1:
return 1
if n == 2:
return 2
return fibonaci2(n - 1) + fibonaci2(n - 2)
# for i in range(1, 100):
# print(fibonaci2(i))
# 4.locals() the most easy way
locals()["x0"] = 1
for i in range(1, 10001):
locals()[f"x{i}"] = locals()[f"x{i - 1}"] + locals()[f"x{i - 2}"] if i > 1 else 2
print(f"方法三:{locals()[f'x{i - 1}']}")
# 5.loop the most basic way
a, b = 0, 1
count, n = 1, 100
while count <= n:
a, b = b, a + b
print(b)
count += 1
"""With large amount of data testing, basic loop is fastest.
And iterator have advantage with the increasing of data amount,
totally the second choice.And locals() and generator are the
same method in the eyes of machine, they can be third choice.
While the recursion is the worst choice anyway,regardless of
resources or speed."""