Skip to content

Commit e03426b

Browse files
committed
Improve and Refactor the fibonnaciSeries.py (Recursion)
1 parent 9ba96c5 commit e03426b

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

Maths/fibonacciSeries.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# Fibonacci Sequence Using Recursion
22

33
def recur_fibo(n):
4-
if n <= 1:
5-
return n
6-
else:
7-
return(recur_fibo(n-1) + recur_fibo(n-2))
4+
return n if n <= 1 else (recur_fibo(n-1) + recur_fibo(n-2))
85

9-
limit = int(input("How many terms to include in fibonacci series: "))
6+
def isPositiveInteger(limit):
7+
return limit >= 0
108

11-
if limit <= 0:
12-
print("Please enter a positive integer: ")
13-
else:
14-
print(f"The first {limit} terms of the fibonacci series are as follows")
15-
for i in range(limit):
16-
print(recur_fibo(i))
9+
def main():
10+
limit = int(input("How many terms to include in fibonacci series: "))
11+
if isPositiveInteger:
12+
print(f"The first {limit} terms of the fibonacci series are as follows:")
13+
print([recur_fibo(n) for n in range(limit)])
14+
else:
15+
print("Please enter a positive integer: ")
16+
17+
if __name__ == '__main__':
18+
main()

0 commit comments

Comments
 (0)