-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10thRecursioninPython.py
More file actions
164 lines (82 loc) · 2.96 KB
/
10thRecursioninPython.py
File metadata and controls
164 lines (82 loc) · 2.96 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# ! Recursion
# * Calling a function while the function itself is running
# * We can use the above property to solve problems
# * To use the above thing, the criteria of writing a recursive functions:
# * Base Case: The condition to stop recursion at this point
# * Recursive Case: Anything which is not the base case but takes some step towards the base case
# * Recursive Fucntions work like STACKS!!!
# * Using Recursive functions to find factorial of a no.
# * Depth: No. of steps to complete a problem
# Advantages:
# 1. Used to simplify complex problems into smaller easier problems
# 2. Solving problems with repeated patterns
# 3. Handling unknown levels of depth
# 4. Mathematical & Algorithmic
def factorial(n):
if n == 0: # * Base Case
return 1
else: # * Recursive Case
return n * factorial(n - 1) # * This calls the function again and again until the argument doesn't become 0 to reach the base case and
# * from there the functins keep repeating their values to their callers coming to this place at last which gives us the answer
print(factorial(5))
# * Using Recursive Function, find GCD b/w two no.
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
print(gcd(91,65))
# * Using Recursive Function, print out the Fibonnaci Series
def fibonnaci(n):
if n <= 1: # * Base Case
return n
else: # * Recursive Case
return fibonnaci(n - 1) + fibonnaci(n - 2) # * Because Fibonnaci Series is basically nth postion no. = (n-2)th position no. + (n-1)th position no.
for i in range(0,6):
print(fibonnaci(i),end = ' ') # * At that index, it returns the fibonnaci no. present there
print()
# * Using Recursive Function, print out the the Movements for the game of Tower of Hanoi
i = 0
def toh(n, src, aux, des):
global i
if n == 1:
i+=1
print(f'Move Disk 1 from source {src} to destination {des}.')
else:
i+=1
toh(n - 1, src, des, aux)
print(f'Move Disk {n} from source {src} to destination {aux}.')
toh(n - 1, aux, src, des)
toh(6,'A','B','C')
print('The no. of moves it takes to solve the Tower of Hanoi',i)
# * Using Recursive function to print a simple pattern
def pattern(n):
if n == 0: # * Base Case
return
print('*')
pattern(n-1)
pattern(5)
# * Using Recursive function to add two no.
def add(x,y):
if y == 0: # * Base Case
return x
return add(x,y-1) + 1
# * Using Recursive function to subtract two no.
def sub(x,y):
if y == 0: # * Base Case
return x
return sub(x - 1,y - 1)
# * Using Recursive function to multiply two no.
def mul(x,y):
if x < y:
return mul(y,x)
elif y != 0:
return x + mul(x, y - 1)
else: # * Base Case
return 0
# * Using Recursive function to divide two no.
def div(x,y):
if x < y: # * Base Case
return 0
else:
return 1 + div(x-y, y)