File tree Expand file tree Collapse file tree 1 file changed +15
-13
lines changed
Fibonacci_Sequence_Generator Expand file tree Collapse file tree 1 file changed +15
-13
lines changed Original file line number Diff line number Diff line change 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
1
+ def fibo (n ):
2
+ if n <= 0 :
3
+ return []
4
+ elif n == 1 :
5
+ return [0 ]
6
+ elif n == 2 :
7
+ return [0 , 1 ]
5
8
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
+ sequence = [0 , 1 ]
10
+ for i in range (2 , n ):
11
+ sequence .append (sequence [i - 1 ] + sequence [i - 2 ])
12
+ return sequence
8
13
9
14
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
+ while n <= 0 :
16
+ n = int (input ("Enter how many terms you need in your Fibonacci Sequence: " ))
13
17
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)
18
+ fib_sequence = fibo (n )
19
+ print (", " .join (map (str , fib_sequence )))
You can’t perform that action at this time.
0 commit comments