-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecursion_4pm.py
96 lines (69 loc) · 1.77 KB
/
recursion_4pm.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Recursion - idea is that functions can call themselves
"""
def my_function(count):
print(count)
if count == 100:
return
my_function(count + 1)
my_function(0)
"""
Factorials
n! = n(n - 1)(n - 2)(n - 3).... 1
0! = 1
To think recursively you ask "what if i knew the answer to 4!?"
5! = 5 * 4!
n! = n * (n - 1)!
"""
def iter_fact(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
"""
rec_fact(5) = 5 * rec_fact(4) = 5 * 24 = 120
rec_fact(4) = 4 * rec_fact(3) = 4 * 6 = 24
rec_fact(3) = 3 * rec_fact(2) = 3 * 2 = 6
rec_fact(2) = 2 * rec_fact(1) = 2 * 1 = 2
rec_fact(1) = 1 * rec_fact(0) = 1 * 1 = 1
rec_fact(0) = 1
"""
def rec_fact(n):
if n == 0:
return 1
print(f'calling recursive factorial on {n - 1}')
result = rec_fact(n - 1) # this is where it gets paused
print(f'returned from recursive factorial on {n - 1}')
return n * result
"""
Second math example: Fibonacci
Fib(n) = Fib(n - 1) + Fib(n - 2)
Fib(1) = 1
Fib(0) = 1
"""
def rec_fib(n):
"""
first example of branching recursion
"""
if n <= 1:
return 1
print(f'fib({n}) Calling fib({n - 1})')
result_1 = rec_fib(n - 1)
print(f'fib({n}) Calling fib({n - 2})')
result_2 = rec_fib(n - 2)
return result_1 + result_2
def iter_fib(n):
if n <= 1:
return 1
prev = 1
prev_prev = 1
current = 1
for i in range(2, n + 1):
current = prev + prev_prev
prev_prev = prev
prev = current
return current
k = int(input(">> "))
while k > -1:
print(iter_fib(k))
k = int(input(">> "))