-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrecursion.py
More file actions
179 lines (122 loc) · 2.85 KB
/
recursion.py
File metadata and controls
179 lines (122 loc) · 2.85 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
###########################################
#printing n times
# import sys
# sys.setrecursionlimit(10)
# print(sys.getrecursionlimit())
def print_(count):
if count == 0:
return
else:
print("something")
count -= 1
print_(count)
# print_(3)
#############################################
#count numbers 1-10
def con_():
for i in range(1,11):
print(i)
#con_()
#counting numbers from 1 - userinput
def conn_(n):
for i in range(1, n + 1):
print(i)
# user_ = int(input("enter the value: "))
#conn_(user_)
#using recursion
def rec_con(n):
if n >= 1:
#print(n)
rec_con(n-1)
print(n)
# return
# else :
# print(n)
# rec_con(n+1)
#return
# user_ = int(input("enter the value: "))
# rec_con(user_)
#############################################
#reversing a string
def rev_():
s = "hello"
i = ""
for item in s:
i = item + i
print(i, end=" ")
#rev_()
#using recursion
x = "hello"
def rec_rev(s):
if len(s) ==0 :
return s
else:
return rec_rev(s[1:]) + s[0]
# print(rec_rev(x))
################################################
#factorial
def fact_(n):
fact = 1
for i in range(1, n + 1):
fact = fact * i
#print(fact)
print(fact)
#fact_(5)
#using while
n = 5
i = 1
fact = 1
while i <= n:
fact = fact * i
i += 1
# print(fact)
def fact_(n, i=1, fact=1):
if i <= n:
fact = fact * i
i += 1
return fact_(n, i, fact)
else:
return fact
# print(fact_(5))
#using recursion
def rec_fact_(n, i = 1):
if n > 1:
i *= n
n = n - 1
return rec_fact_(i,n)
# print(rec_fact_(3))
# string reversal
i = 0
s = "hello"
res = ""
while i < len(s):
res = s[i] + res
i += 1
print(res)
def string_reversal(string, i=0, res=""):
if i < len(string):
res = s[i] + res
return string_reversal(string, i+1, res)
return res
# if not (i < len(string)):
# return res
#
# res = s[i] + res
# return string_reversal(string, i+1, res)
#print(string_reversal("hello"))
################################################
#fibonacci series
def recursive_fibonacci(n):
if n <= 1:
return n
else:
return (recursive_fibonacci(n - 1) + recursive_fibonacci(n - 2))
n_terms = int(input("enter the range of fibonacci series: "))
# check if the number of terms is valid
if n_terms < 0:
print("Invalid input ! Please input a positive value")
else:
print("Fibonacci series:")
for i in range(n_terms):
print(recursive_fibonacci(i))
##################################################