forked from TisolaNox/EcoChallenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibo.py
53 lines (37 loc) · 1.08 KB
/
fibo.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
import time
# Fibonacci function using iteration
import click
def fibonacci_iterative(n):
start_time = time.time()
if n <= 2:
return 1 if n else 0
else:
a = 0
b = 1
for i in range(2, n + 1):
a, b = b, a + b
end_time = time.time()
print(f"Time taken by iterative function: {end_time - start_time} seconds")
return b
# Fibonacci function using recursion
def fibonacci_recursive(n):
start_time = time.time()
if n <= 2:
return 1 if n else 0
else:
result = fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)
end_time = time.time()
print(f"Time taken by recursive function: {end_time - start_time} seconds")
return result
@click.command()
@click.option('--mode', type=click.Choice(['rec', 'ite']), default='ite')
@click.option('--n', default=10)
def main(mode: str, n: int):
fibonacci_functions = {
'rec': fibonacci_recursive,
'ite': fibontacci_iterative
}
result = fibonacci_functions.get(mode)(n)
print(result)
if __name__ == "__main__":
main()