File tree Expand file tree Collapse file tree 1 file changed +15
-21
lines changed
Fibonacci_Sequence_Generator Expand file tree Collapse file tree 1 file changed +15
-21
lines changed Original file line number Diff line number Diff line change 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
9
8
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
15
13
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)
You can’t perform that action at this time.
0 commit comments