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 9194c5a

Browse files
Merge pull request avinashkranjan#1604 from DevBhojani116/master
Updated Fibonacci.py using recursion
2 parents 9c9eab2 + 098c04a commit 9194c5a

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed
Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1-
def Fibbo_Sequence_Generator():
2-
# Generates a fibonacci sequence with the size of ngi
3-
runFib = True
4-
while(runFib):
5-
n = int(input('How many numbers do you need? '))
6-
if n > 0 :
7-
runFib = False
8-
series = [1]
1+
#function which finds out individual term of the fibonacci sequence
2+
def fibo(x):
3+
if(x == 1 or x == 2): #base case
4+
return 1
5+
else:
6+
return fibo(x-1) + fibo(x-2)
7+
#following the actual way how the fibonacci series is generated i.e. adding the previous two terms
98

10-
while len(series) < n:
11-
if len(series) == 1:
12-
series.append(1)
13-
else:
14-
series.append(series[-1] + series[-2])
9+
n = 0
10+
while(n<=0):
11+
n = int(input("Enter how many terms do you need in your Fibonacci Sequence? "))
12+
#this loop is just to ensure that the user inputs a valid number
1513

16-
for i in range(len(series)): # Convert the numbers to strings
17-
series[i] = str(series[i])
18-
else:
19-
print('enter a valid number')
20-
21-
return(', '.join(series)) # Return the sequence seperated by commas
22-
23-
print(Fibbo_Sequence_Generator())
14+
for i in range(1,n):
15+
#finding out individual terms of the series and printing till (n-1) terms since we don't want an extra comma at the end
16+
print(fibo(i), end=", ")
17+
print(fibo(n)) #printing the last(i.e. nth term)

0 commit comments

Comments
(0)

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