-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recursion.py
60 lines (43 loc) · 874 Bytes
/
Recursion.py
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
#calling the function/method itself inside the function
# def sum(i):
# return i + sum(i-1)
# fibanocci series
# 1 1 2 3 5 8 ......
# fib(1) = 1
# fib(2) = 1, 1
# fib(3) = 1, 1, 2
# fib(4) = 1, 1, 2, 3
# fib(5) = 1, 1, 2, 3, 5
#
# fib(5) = fib(4), 5
# = fib(3), 4 , 5
# = fib(2), 3, 4 , 5
# = fib(1), 2 ,3 ,4, 5
#factorial of a number
# fact(1) = 1
#fact(2) = 2 * 1
# fact(3) = 3 * 2 * 1
# fact(4) = 4 * 3 * 2 * 1
# fact(4) = 4 * fact(3)
# = 4 * 3 * fact(2)
# = 4 * 3 * 2 * fact(1)
# = 4 * 3 * 2 * 1
#with recursion
def fact(n):
if n == 1:
return 1
return n * fact(n-1)
#without recursion
n = 3
factorial = 1
for i in range(1,n+1):
factorial = factorial * i
print(factorial)
# res = fact(6)
#
# print(res)
# fact(1)
#
# fact(2)
res = fact(3)
print(res)