-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion-basic-level.cpp
More file actions
216 lines (174 loc) Β· 3.93 KB
/
Copy pathrecursion-basic-level.cpp
File metadata and controls
216 lines (174 loc) Β· 3.93 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// πΉ 1. Factorial
// π Mathematical Definition
// n!=nΓ(nβ1)Γ(nβ2)Γβ―Γ1
// Base case:
// 0!=1
// π Recursive Idea
// n!=nΓ(nβ1)!
// π Each function call waits for the next smaller factorial.
// #include<bits/stdc++.h>
// using namespace std;
// int fact(int n){
// if(n==0) return 1;
// return n*(fact(n-1));
// }
// int main(){
// int n;cin>>n;
// cout<<fact(n)<<endl;
// return 0;
// }
// πΉ 2. Fibonacci
// π Mathematical Definition
// F(n)=F(nβ1)+F(nβ2)
// Base cases:
// F(0)=0,F(1)=1
// π Recursive Idea
// Each call splits into two subproblems.
// π§ Call Stack (Example: F(4))
// fib(4)
// β fib(3) + fib(2)
// β (fib(2)+fib(1)) + (fib(1)+fib(0))
// #include<bits/stdc++.h>
// using namespace std;
// int fibonacci(int n){
// if(n==0) return 0;
// if(n==1) return 1;
// return fibonacci(n-1) +fibonacci(n-2);
// }
// int main(){
// int n;
// cin>>n;
// cout<<fibonacci(n)<<endl;
// return 0;
// }
// πΉ 3. Fibonacci with Debugging
// #include <iostream>
// using namespace std;
// int fibonacci(int n) {
// if (n == 0) {
// cout << "fib(0) = 0" << endl;
// return 0;
// }
// if (n == 1) {
// cout << "fib(1) = 1" << endl;
// return 1;
// }
// int result = fibonacci(n - 1) + fibonacci(n - 2);
// cout << "fib(" << n << ") = " << result << endl;
// return result;
// }
// int main() {
// int n;
// cin>>n;
// fibonacci(n);
// return 0;
// }
// πΉ 3. Sum of First N Numbers
// π Mathematical Definition
// S(n)=1+2+3+β―+n
// Closed form:
// S(n)=n(n+1)/2
// π Recursive Idea
// S(n)=n+S(nβ1)
// Base case:
// S(0)=0
// Each call waits for the sum of the first n-1 numbers.
// S(n)=n+S(nβ1)
// #include<bits/stdc++.h>
// using namespace std;
// int sum(int n){
// if(n==0) return 0;
// return n+sum(n-1);
// }
// int main(){
// int n;
// cin>>n;
// cout<<sum(n)<<endl;
// return 0;
// }
// πΉ 4. Power Function
// πΉ 4. Power (a^b)
// π Mathematical Definition
// //power(a,b)=aΓaΓaΓβ―Γa (b times)
// Base case:
// power(a,0)=1
// π Recursive Idea
// power(a,b)=aΓpower(a,bβ1)
// π§ Flow (Example: 2Β³)
// power(2,3)
// β 2 * power(2,2)
// β 2 * (2 * power(2,1))
// β 2 * 2 * 2 * power(2,0)
// β 8
// #include<bits/stdc++.h>
// using namespace std;
// int power(int a,int b){
// if(b==0) return 1;
// return a*power(a,b-1);
// }
// int main(){
// int a,b;
// cin>>a>>b;
// cout<<power(a,b)<<endl;
// return 0;
// }
// πΈ 1. Print 1 to N
// π Mathematical Idea
// We want:
// 1,2,3,β¦,N
// π Recursive Relation
// print(n)=print(nβ1)+n
// π First solve smaller problem, then print current.
// #include<bits/stdc++.h>
// using namespace std;
// void print(int n){
// if(n==0) return;
// print(n-1);
// cout<<n<<" ";// After the recursive call, we print n.
// }
// int main(){
// int n;
// cin>>n;
// print(n);
// return 0;
// }
// πΈ 2. Print N to 1
// π Mathematical Idea
// We want:
// N,Nβ1,Nβ2,β¦,1
// π Recursive Relation
// print(n)=n+print(nβ1)
// π Print current first, then solve smaller problem.
// #include<bits/stdc++.h>
// using namespace std;
// void print(int n){
// if(n==0) return;
// cout<<n<<" ";// First we print n, then we make the recursive call.
// print(n-1);
// }
// int main(){
// int n;
// cin>>n;
// print(n);
// return 0;
// }
// 3. Print Name N Times
// π Mathematical Idea
// Repeat a string N times
// f(n)=name+f(nβ1)
// π Recursive Relation
// print(n)=print(nβ1)+"name"
// #include<bits/stdc++.h>
// using namespace std;
// void print(string name,int n){
// if(n==0) return;
// print(name,n-1);
// cout<<name<<" ";// After the recursive call, we print the name.
// }
// int main(){
// string name;
// int n;
// cin>>name>>n;
// print(name,n);
// return 0;
// }