You might have heard about different types of series of numbers. In this article, we will discuss such a series named the Fibonacci series. The Fibonacci series has a lot of significance in financial markets as it gives a golden ratio that helps in determining the direction towards which any financial market will move. We will also implement a program to find the Fibonacci series in python.
What Is the Fibonacci Series?
The Fibonacci series has been named after the Italian mathematician Fibonacci. In a Fibonacci series, any number at position N is defined as the sum of numbers at position (N-1) and (N-2).
The first and second term of the Fibonacci series has been defined as 0 and 1.
Mathematically, A Fibonacci series F can be defined as follows.
F1=0
F2=1
FN=FN-1+FN-2
Using the above formulae, we can find the number at any position in the Fibonacci Series. For instance,
F3=F2+F1
=1+0
=1
F4=F3+F2
=1+1
=2
We can find the number at any position in the Fibonacci series using the above formula.
How To Determine Fibonacci Series In Python?
To determine the Fibonacci series in python, we can simply use the methodology used above. We can start with the first and second terms and find other terms in the Fibonacci series using a for loop or while loop in python.
For instance, to find the number at the Nth position in the Fibonacci series, we will execute a while loop N-2 times to calculate the terms from the 3rd position to the Nth position. To store the terms, we will use a python list.
fibonacciList = [0, 1]
# finding 10 terms of the series starting from 3rd term
N = 10
term = 3
while term < N + 1:
value = fibonacciList[term - 2] + fibonacciList[term - 3]
fibonacciList.append(value)
term = term + 1
print("10 terms of the fibonacci series are:")
print(fibonacciList)
Output:
10 terms of the fibonacci series are:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Instead of using a while loop, we can also use a for loop to determine the Fibonacci series in Python as follows.
fibonacciList = [0, 1]
# finding 10 terms of the series starting from 3rd term
N = 10
for term in range(3, N + 1):
value = fibonacciList[term - 2] + fibonacciList[term - 3]
fibonacciList.append(value)
print("10 terms of the fibonacci series are:")
print(fibonacciList)
Output:
10 terms of the fibonacci series are:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Determine Fibonacci Series Using Recursion In Python
You might be knowing that we can solve a problem using recursion if we can break the problem into smaller sub-problems. As we define a term in the Fibonacci series using its previous terms, we can easily create a recursive solution for determining the term at any position in the Fibonacci series using recursion in Python.
In the recursive solution, we will define a function Fibonacci() that takes a number N as input and returns the term at the Nth position in the Fibonacci series.
For N=1, the function returns 0 while it returns 1 for N=2. For any other value of N, Fibonacci(N) returns the sum of Fibonacci(N-1) and Fibonacci(N-2). We can implement the solution in python as follows.
def fibonacci(N):
if N == 1:
return 0
if N == 2:
return 1
return fibonacci(N - 1) + fibonacci(N - 2)
print("10th term of the fibonacci series is:")
print(fibonacci(10))
Output:
10th term of the fibonacci series is:
34
Conclusion
In this article, we have discussed the Fibonacci series. We have also discussed ways to determine the Fibonacci series in python. To read more about numbers in python, you can read this article on decimal numbers in python. You might also like this article on complex numbers in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.