-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion.py
More file actions
56 lines (46 loc) · 1.2 KB
/
Copy pathRecursion.py
File metadata and controls
56 lines (46 loc) · 1.2 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
# Without recursion
def factorial(n):
result = 1
while n > 1:
result = n * result
n -= 1
return result
print(factorial(5))
# With recursion
def factorial_recursion(n):
if n==1 or n==0:
return 1
return n * factorial_recursion(n-1)
print(factorial_recursion(5))
# Fibonacci series
def fibonacci(n):
# Define the base case
if n <= 1:
return n
else:
# Call recursively to fibonacci
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(6))
cache = [None]*(100)
def fibonacci(n):
if n <= 1:
return n
# Check if the value exists
if not cache[n]:
# Save the result in cache
cache[n] = fibonacci(n-1) + fibonacci(n-2)
return cache[n]
print(fibonacci(6))
# Tower of hanoi
def hanoi(num_disks, from_rod, to_rod, aux_rod):
# Correct the base case
if num_disks <= 1:
# Correct the calls to the hanoi function
hanoi(num_disks, from_rod, aux_rod, to_rod)
print("Moving disk", num_disks, "from rod", from_rod,"to rod",to_rod)
hanoi(num_disks, aux_rod, to_rod, from_rod)
num_disks = 4
source_rod = 'A'
auxiliar_rod = 'B'
target_rod = 'C'
hanoi(num_disks, source_rod, target_rod, auxiliar_rod)