Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9829ba4

Browse files
initial commit
0 parents commit 9829ba4

File tree

276 files changed

+29433
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

276 files changed

+29433
-0
lines changed
Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## 3 Basic steps of Recursion:\n",
8+
"1) Base Case\n",
9+
"2) Induction Hypothesis\n",
10+
"3) Induction Step"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 1,
16+
"metadata": {},
17+
"outputs": [],
18+
"source": [
19+
"def fact(n):\n",
20+
" if n == 0:\n",
21+
" return 1\n",
22+
" return n*fact(n-1)"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": null,
28+
"metadata": {},
29+
"outputs": [],
30+
"source": [
31+
"def fact(n):\n",
32+
" if n == 0:\n",
33+
" return 1\n",
34+
" smallOutput = fact(n-1)\n",
35+
" return n*smallOutput"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": 3,
41+
"metadata": {},
42+
"outputs": [
43+
{
44+
"name": "stdout",
45+
"output_type": "stream",
46+
"text": [
47+
"58\n"
48+
]
49+
},
50+
{
51+
"data": {
52+
"text/plain": [
53+
"2350561331282878571829474910515074683828862318181142924420699914240000000000000"
54+
]
55+
},
56+
"execution_count": 3,
57+
"metadata": {},
58+
"output_type": "execute_result"
59+
}
60+
],
61+
"source": [
62+
"n = int(input())\n",
63+
"fact(n)"
64+
]
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"metadata": {},
69+
"source": [
70+
"### Sum of 'n' natural numbers:"
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": 11,
76+
"metadata": {},
77+
"outputs": [],
78+
"source": [
79+
"def sum(n):\n",
80+
" if n == 1:\n",
81+
" return 1\n",
82+
" return n + sum(n-1)"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": 13,
88+
"metadata": {},
89+
"outputs": [
90+
{
91+
"name": "stdout",
92+
"output_type": "stream",
93+
"text": [
94+
"10\n",
95+
"5050\n",
96+
"500500\n"
97+
]
98+
}
99+
],
100+
"source": [
101+
"n = int(input())\n",
102+
"sum(n)\n",
103+
"print(sum(n + 90))\n",
104+
"print(sum(n + 990))"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": null,
110+
"metadata": {},
111+
"outputs": [],
112+
"source": [
113+
"def sum(n):\n",
114+
" if n == 1:\n",
115+
" return 1\n",
116+
" smallOutput = sum(n-1)\n",
117+
" return n + smallOutput"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": 14,
123+
"metadata": {},
124+
"outputs": [
125+
{
126+
"name": "stdout",
127+
"output_type": "stream",
128+
"text": [
129+
"10\n",
130+
"55\n",
131+
"3628800\n"
132+
]
133+
}
134+
],
135+
"source": [
136+
"n = int(input())\n",
137+
"print(sum(n))\n",
138+
"print(fact(n))"
139+
]
140+
},
141+
{
142+
"cell_type": "markdown",
143+
"metadata": {},
144+
"source": [
145+
"### Power of a number"
146+
]
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": 54,
151+
"metadata": {},
152+
"outputs": [
153+
{
154+
"name": "stdout",
155+
"output_type": "stream",
156+
"text": [
157+
"2 5\n",
158+
"1\n",
159+
"2\n",
160+
"3\n",
161+
"4\n",
162+
"5\n",
163+
"None\n"
164+
]
165+
}
166+
],
167+
"source": [
168+
"str = input().split()\n",
169+
"x, n = int(str[0]), int(str[1])\n",
170+
"\n",
171+
"def power(x,n):\n",
172+
" if n == 0:\n",
173+
" return 1\n",
174+
" power(x,n-1)\n",
175+
" print(n)\n",
176+
" x = x * x\n",
177+
" \n",
178+
"print(power(x,n))"
179+
]
180+
},
181+
{
182+
"cell_type": "code",
183+
"execution_count": null,
184+
"metadata": {},
185+
"outputs": [],
186+
"source": [
187+
"def func(num):\n",
188+
" return func(num-1)\n",
189+
"num = 5\n",
190+
"ans = func(num-1)\n",
191+
"print(ans)\n",
192+
"\n",
193+
"# Results in Recursion Error"
194+
]
195+
},
196+
{
197+
"cell_type": "markdown",
198+
"metadata": {},
199+
"source": [
200+
"### Print numbers from 1 to n"
201+
]
202+
},
203+
{
204+
"cell_type": "code",
205+
"execution_count": 49,
206+
"metadata": {},
207+
"outputs": [],
208+
"source": [
209+
"def print_1_to_n(n):\n",
210+
" if n == 0:\n",
211+
" return n\n",
212+
" print_1_to_n(n-1)\n",
213+
" print(n)"
214+
]
215+
},
216+
{
217+
"cell_type": "code",
218+
"execution_count": 51,
219+
"metadata": {},
220+
"outputs": [
221+
{
222+
"name": "stdout",
223+
"output_type": "stream",
224+
"text": [
225+
"10\n",
226+
"1\n",
227+
"2\n",
228+
"3\n",
229+
"4\n",
230+
"5\n",
231+
"6\n",
232+
"7\n",
233+
"8\n",
234+
"9\n",
235+
"10\n"
236+
]
237+
}
238+
],
239+
"source": [
240+
"n = int(input())\n",
241+
"print_1_to_n(n)"
242+
]
243+
},
244+
{
245+
"cell_type": "code",
246+
"execution_count": 56,
247+
"metadata": {},
248+
"outputs": [],
249+
"source": [
250+
"def print_n_to_1(n):\n",
251+
" if n == 0:\n",
252+
" return n\n",
253+
" print(n)\n",
254+
" print_n_to_1(n-1)"
255+
]
256+
},
257+
{
258+
"cell_type": "code",
259+
"execution_count": 57,
260+
"metadata": {},
261+
"outputs": [
262+
{
263+
"name": "stdout",
264+
"output_type": "stream",
265+
"text": [
266+
"10\n",
267+
"10\n",
268+
"9\n",
269+
"8\n",
270+
"7\n",
271+
"6\n",
272+
"5\n",
273+
"4\n",
274+
"3\n",
275+
"2\n",
276+
"1\n"
277+
]
278+
}
279+
],
280+
"source": [
281+
"n = int(input())\n",
282+
"print_n_to_1(n)"
283+
]
284+
},
285+
{
286+
"cell_type": "code",
287+
"execution_count": 58,
288+
"metadata": {},
289+
"outputs": [
290+
{
291+
"name": "stdout",
292+
"output_type": "stream",
293+
"text": [
294+
"5 3 1 "
295+
]
296+
}
297+
],
298+
"source": [
299+
"#Predict the Output:\n",
300+
"def printNumbers(n):\n",
301+
" if(n<0):\n",
302+
" return\n",
303+
" print(n,end=\" \")\n",
304+
" printNumbers(n-2)\n",
305+
"num = 5\n",
306+
"printNumbers(num)"
307+
]
308+
},
309+
{
310+
"cell_type": "code",
311+
"execution_count": null,
312+
"metadata": {},
313+
"outputs": [],
314+
"source": []
315+
}
316+
],
317+
"metadata": {
318+
"kernelspec": {
319+
"display_name": "Python 3",
320+
"language": "python",
321+
"name": "python3"
322+
},
323+
"language_info": {
324+
"codemirror_mode": {
325+
"name": "ipython",
326+
"version": 3
327+
},
328+
"file_extension": ".py",
329+
"mimetype": "text/x-python",
330+
"name": "python",
331+
"nbconvert_exporter": "python",
332+
"pygments_lexer": "ipython3",
333+
"version": "3.7.3"
334+
}
335+
},
336+
"nbformat": 4,
337+
"nbformat_minor": 2
338+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /