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
| import datetime
start = datetime.datetime.now() pre = 0 cur = 1 print(pre, cur, end=' ') n = 35
for i in range(n-1): pre, cur = cur, pre + cur print(cur, end=' ') delta = (datetime.datetime.now() - start).total_seconds() print(delta)
start = datetime.datetime.now() pre = 0 cur = 1 print(pre, cur, end=' ')
def fib1(n, pre=0,cur=1): pre, cur = cur, pre + cur print(cur, end=' ') if n == 2: return fib1(n-1, pre, cur)
fib1(n) delta = (datetime.datetime.now() - start ).total_seconds() print(delta)
start = datetime.datetime.now() def fib2(n): if n < 2: return 1 return fib2(n-1) + fib2(n-2)
for i in range(n): print(fib2(i), end=' ') delta = (datetime.datetime.now() - start).total_seconds() print(delta)
|