-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.py
More file actions
86 lines (74 loc) · 2.05 KB
/
fibonacci.py
File metadata and controls
86 lines (74 loc) · 2.05 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
#!/usr/bin/env python
# encoding: utf-8
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Copyright (c) 2019-2022 Vladimir Shurygin. All rights reserved.
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
Given an integer n, write a function to compute the nth Fibonacci number.
"""
import argparse
import random
def fibonacci(n):
"""it is naive implementation which takes exponential time to execute.
BigO=2**(n/2)
"""
if n <= 1:
return 1
n0 = 0
n1 = 1
result = 1
level = 2
while level <= n:
result = n0 + n1
n0 = n1
n1 = result
level += 1
return result
_MEMO = {}
def fibonacci_with_hash(n):
"""
it use memoize dynamic programming algorithm
BigO = n
"""
if n in _MEMO:
return _MEMO[n]
if n <= 2:
return 1
f = fibonacci_with_hash(n - 1) + fibonacci_with_hash(n - 2)
_MEMO[n] = f
return f
def fib_bottom_up(n):
"""
dynamic programming bottom-up algorithm
idea of algorithm is start solving program from bottom and go up,
may uses in junction with memoize DP
it use array as a cache
BigO = n
"""
_memo = [1] * n # init array
fib = 1
for arr_inx in range(0, n):
if arr_inx <= 1:
fib = 1
else:
fib = _memo[arr_inx - 1] + _memo[arr_inx - 2]
_memo[arr_inx] = fib
return fib
def get_context():
""" Create execution context command line args
Returns
-------
Object
object with command line arguments """
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--number",
help="fibonacci level",
dest="number", type=int, default=random.randrange(0, 24))
return parser.parse_args()
if __name__ == '__main__':
context = get_context()
level = context.number
result = fibonacci(level)
print(f'fibonacci({level}) = {result}')
result = fib_bottom_up(level)
print(f'fibonacci({level}) = {result}')