A fibonacci series is a special number of series which follow a pattern in which the next number of series is the sum of the previous two numbers. The Fibonacci series starts with two numbers 0 and 1. There is are two rules we need to follow to create a Fibonacci series:
- The series must start with 0 and 1 or 1 and 1.
- The next number must the sum of the previous two numbers
Fibonacci series for 1 st 7 numbers:
If Fibonacci series starts with 0 and 1 0,1, 1 2, 3 ,5, 8 If the Fibonacci series starts with 1 and 1. 1,1,2,3,5,8,13
Implementation of Fibonacci
As we know that Fibonacci is a series that follow simple logic, in which we need the sum of the previous two numbers again and again in order to find the next number of the sequence. So, in programming language for repetitive statements, we can either use iteration such as loop or recursion. Here we have implemented the Fibonacci series using both Iterations as well as recursion:
Python Implementation of Fibonacci Series using Iterative:
def fibonacci(n): a = 0 b = 1 print(0,end='') for i in range(n-1): print("-->",b, end='') a,b = b, a+b num = int(input("Enter the length of fibonacci series: ")) fibonacci(num)
Python Implementation of Fibonacci Series using recursion:
def fibonacci(n,a=0,b=1): if n==1: return print('-->',b,end="") fibonacci(n-1,b,a+b) num = int(input("Enter the length of fibonacci series: ")) print(0,end='') fibonacci(num)
Output:
Enter the length of fibonacci series: 8 0--> 1--> 1--> 2--> 3--> 5--> 8--> 13
People are also reading: