-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursion.py
91 lines (66 loc) · 1.44 KB
/
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
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
'''
# recursion
def recu(check: int):
if check == 1:
print("base condition")
else:
return recu(check-1)
def firstMethod():
secondMethod()
print("I am the first Method")
def secondMethod():
thirdMethod()
print("I am the second Method")
def thirdMethod():
fourthMethod()
print("I am the third Method")
def fourthMethod():
print("I am the fourth Method")
def fibonacci(num: int):
if num <= 0:
return 0
elif num == 1:
return 1
else:
return fibonacci(num - 1) + fibonacci(num - 2)
def factorial(num: int):
if num == 1:
return 1
else:
return num * factorial(num - 1)
def power_of_two(n):
if n == 0:
return 1
else:
power = power_of_two(n-1)
return power * 2
def check_power_of_two(num: int):
while num % 2 == 0:
num = num / 2
if num ==1:
return True
else:
return False
def isPowerOfTwo(num: int) -> bool:
if num == 1:
return True
if num > 1 and num % 2 == 0:
return isPowerOfTwo(int(num/2))
else:
return False
'''
def pri_num(num: int):
if num == 5:
print(5)
else:
print(num)
pri_num(num + 1)
if __name__ == '__main__':
print(pri_num(16))
# print(check_power_of_two(16))
#print(factorial(4))
# print(fibonacci(3))
# recu(5)
#print(power_of_two(4))
# firstMethod()
# pri_num(1)