1
+
2
+ # ! Recursion
3
+
4
+ # * Calling a function while the function itself is running
5
+
6
+ # * We can use the above property to solve problems
7
+
8
+ # * To use the above thing, the criteria of writing a recursive functions:
9
+
10
+ # * Base Case: The condition to stop recursion at this point
11
+
12
+ # * Recursive Case: Anything which is not the base case but takes some step towards the base case
13
+
14
+ # * Recursive Fucntions work like STACKS!!!
15
+
16
+ # * Using Recursive functions to find factorial of a no.
17
+
18
+ # * Depth: No. of steps to complete a problem
19
+
20
+ # Advantages:
21
+
22
+ # 1. Used to simplify complex problems into smaller easier problems
23
+
24
+ # 2. Solving problems with repeated patterns
25
+
26
+ # 3. Handling unknown levels of depth
27
+
28
+ # 4. Mathematical & Algorithmic
29
+
30
+ def factorial (n ):
31
+
32
+ if n == 0 : # * Base Case
33
+
34
+ return 1
35
+
36
+ else : # * Recursive Case
37
+
38
+ 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
39
+
40
+ # * from there the functins keep repeating their values to their callers coming to this place at last which gives us the answer
41
+
42
+ print (factorial (5 ))
43
+
44
+ # * Using Recursive Function, find GCD b/w two no.
45
+
46
+ def gcd (a , b ):
47
+
48
+ if b == 0 :
49
+
50
+ return a
51
+
52
+ else :
53
+
54
+ return gcd (b , a % b )
55
+
56
+ print (gcd (91 ,65 ))
57
+
58
+ # * Using Recursive Function, print out the Fibonnaci Series
59
+
60
+ def fibonnaci (n ):
61
+
62
+ if n <= 1 : # * Base Case
63
+
64
+ return n
65
+
66
+ else : # * Recursive Case
67
+
68
+ 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.
69
+
70
+ for i in range (0 ,6 ):
71
+
72
+ print (fibonnaci (i ),end = ' ' ) # * At that index, it returns the fibonnaci no. present there
73
+
74
+ print ()
75
+
76
+ # * Using Recursive Function, print out the the Movements for the game of Tower of Hanoi
77
+
78
+ i = 0
79
+
80
+ def toh (n , src , aux , des ):
81
+
82
+ global i
83
+
84
+ if n == 1 :
85
+
86
+ i += 1
87
+
88
+ print (f'Move Disk 1 from source { src } to destination { des } .' )
89
+
90
+ else :
91
+
92
+ i += 1
93
+
94
+ toh (n - 1 , src , des , aux )
95
+
96
+ print (f'Move Disk { n } from source { src } to destination { aux } .' )
97
+
98
+ toh (n - 1 , aux , src , des )
99
+
100
+ toh (6 ,'A' ,'B' ,'C' )
101
+
102
+ print ('The no. of moves it takes to solve the Tower of Hanoi' ,i )
103
+
104
+ # * Using Recursive function to print a simple pattern
105
+
106
+ def pattern (n ):
107
+
108
+ if n == 0 : # * Base Case
109
+
110
+ return
111
+
112
+ print ('*' )
113
+
114
+ pattern (n - 1 )
115
+
116
+ pattern (5 )
117
+
118
+ # * Using Recursive function to add two no.
119
+
120
+ def add (x ,y ):
121
+
122
+ if y == 0 : # * Base Case
123
+
124
+ return x
125
+
126
+ return add (x ,y - 1 ) + 1
127
+
128
+ # * Using Recursive function to subtract two no.
129
+
130
+ def sub (x ,y ):
131
+
132
+ if y == 0 : # * Base Case
133
+
134
+ return x
135
+
136
+ return sub (x - 1 ,y - 1 )
137
+
138
+ # * Using Recursive function to multiply two no.
139
+
140
+ def mul (x ,y ):
141
+
142
+ if x < y :
143
+
144
+ return mul (y ,x )
145
+
146
+ elif y != 0 :
147
+
148
+ return x + mul (x , y - 1 )
149
+
150
+ else : # * Base Case
151
+
152
+ return 0
153
+
154
+ # * Using Recursive function to divide two no.
155
+
156
+ def div (x ,y ):
157
+
158
+ if x < y : # * Base Case
159
+
160
+ return 0
161
+
162
+ else :
163
+
164
+ return 1 + div (x - y , y )
0 commit comments