Fibonacci Series In Python
Python Program to Print the Fibonacci sequence
In this program, you’ll learn to print the Fibonacci sequence using a while loop.
To understand this example, you should have knowledge of the following Python programming topics:
- Python if…else Statement
- Python while Loop
A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8….
The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms. This means to say the nth term is the sum of (n-1)th and (n-2)th term.
Source Code
# Program to display the Fibonacci sequence up to n-th term
nterms = int(input("How many terms? "))
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Output
How many terms? 7 Fibonacci sequence: 0 1 1 2 3 5 8
Here, we store the number of terms in nterms. We initialize the first term to 0 and the second term to 1.
If the number of terms is more than 2, we use a while
loop to find the next term in the sequence by adding the preceding two terms. We then interchange the variables (update it) and continue on with the process.
You can also solve this problem using recursion: Python program to print the Fibonacci sequence using recursion.
The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
Fn = Fn-1 + Fn-2
with seed values
F0 = 0 and F1 = 1.
Method 1 ( Use recursion ) :
- Python
# Function for nth Fibonacci number def Fibonacci(n): # Check if input is 0 then it will # print incorrect input if n < 0 : print ( "Incorrect input" ) # Check if n is 0 # then it will return 0 elif n = = 0 : return 0 # Check if n is 1,2 # it will return 1 elif n = = 1 or n = = 2 : return 1 else : return Fibonacci(n - 1 ) + Fibonacci(n - 2 ) # Driver Program print (Fibonacci( 9 )) # This code is contributed by Saket Modi # then corrected and improved by Himanshu Kanojiya |
34
Method 2 ( Use Dynamic Programming ) :
# Function for nth fibonacci # number - Dynamic Programing # Taking 1st two fibonacci nubers as 0 and 1 FibArray = [ 0 , 1 ] def fibonacci(n): # Check is n is less # than 0 if n < = 0 : print ( "Incorrect input" ) # Check is n is less # than len(FibArray) elif n < = len (FibArray): return FibArray[n - 1 ] else : temp_fib = fibonacci(n - 1 ) + fibonacci(n - 2 ) FibArray.append(temp_fib) return temp_fib # Driver Program print (fibonacci( 9 )) # This code is contributed by Saket Modi |
21
Method 3 ( Space Optimized):
- Python
# Function for nth fibonacci # number - Space Optimisataion # Taking 1st two fibonacci numbers as 0 and 1 def fibonacci(n): a = 0 b = 1 # Check is n is less # than 0 if n < 0 : print ( "Incorrect input" ) # Check is n is equal # to 0 elif n = = 0 : return 0 # Check if n is equal to 1 elif n = = 1 : return b else : for i in range ( 1 , n): c = a + b a = b b = c return b # Driver Program print (fibonacci( 9 )) # This code is contributed by Saket Modi # Then corrected and improved by Himanshu Kanojiya |
34
Please refer complete article on Program for Fibonacci numbers for more details!
How do you do the Fibonacci series in Python?
- INPUT FORMAT: Input consists of an integer.
- OUTPUT FORMAT: …
- SAMPLE INPUT: 7.
- SAMPLE OUTPUT: 0 1 1 2 3 5 8.
- PREREQUISITE KNOWLEDGE: while loop in Python and Recursion in Python. …
- Step 1:Input the ‘n’ value until which the Fibonacci series has to be generated.
- Step 3:while (count <= n)
- Step 5:Increment the count variable.
How do you find the nth Fibonacci number in Python?
- def fibonacci(n):
- if n <= 1:
- return n.
- else:
- return(fibonacci(n-1) + fibonacci(n-2))
- print(Fibonacci(4))
What is the Fibonacci Series formula?
What is the logic of the Fibonacci series?
Is 0 a Fibonacci number?
How do you create Fibonacci numbers?
…
The next number is found by adding up the two numbers before it:
- the 2 is found by adding the two numbers before it (1+1),
- the 3 is found by adding the two numbers before it (1+2),
- the 5 is (2+3),
- and so on!
What is nth Fibonacci number?
What is the use of the Fibonacci series?
How do you find a number that is Fibonacci or not?
What is the biggest Fibonacci number?
3340367
How do you use the Fibonacci equation?
This will give you the second number in the sequence. Remember, to find any given number in the Fibonacci sequence, you simply add the two previous numbers in the sequence. To create the sequence, you should think of 0 coming before 1 (the first term), so 1 + 0 = 1.