# NumPy – Fibonacci Series using Binet Formula

All of us are familiar with Fibonacci Series. Each number in the sequence is the sum of the two numbers that precede it. So, the sequence goes: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34…… In this tutorial, we will implement the same using NumPy with the aid of Binet formula.

Binet Formula n’ is the parameter which relates the first ‘n’ numbers of Fibonacci Series. In the first example we are going to findout first 10 numbers of Fibonacci Series (n = 10), after that we takes the parameter ‘n’ from user and produce the corresponding result.

NOTE : We are ignoring the first element(0) of Fibonacci Series

Example 1: To find first 10 Fibonacci numbers .

 import numpy as np      # We are creating an array contains n = 10 elements  # for getting first 10 Fibonacci numbers  a = np.arange(1, 11)  lengthA = len(a)     # splitting of terms for easiness  sqrtFive = np.sqrt(5)  alpha = (1 + sqrtFive) / 2 beta = (1 - sqrtFive) / 2    # Implementation of formula  # np.rint is used for rounding off to integer  Fn = np.rint(((alpha ** a) - (beta ** a)) / (sqrtFive))  print("The first {} numbers of Fibonacci series are {} . ".format(lengthA, Fn))

Output :

The first 10 numbers of Fibonacci series are [ 1. 1. 2. 3. 5. 8. 13. 21. 34. 55.] .

Example 2 : To find first ‘n’ Fibonacci numbers ..

 import numpy as np     # We are creating an array contains n elements  # for getting first 'n' Fibonacci numbers  fNumber = int(input("Enter the value of n + 1'th number : "))  a = np.arange(1, fNumber)  length_a = len(a)     # splitting of terms for easiness  sqrt_five = np.sqrt(5)  alpha = (1 + sqrt_five) / 2 beta = (1 - sqrt_five) / 2    # Implementation of formula  # np.rint is used for rounding off to integer  Fn = np.rint(((alpha ** a) - (beta ** a)) / (sqrt_five))  print("The first {} numbers of Fibonacci series are {} . ".format(length_a, Fn))

Output :

# Here user input was 10
Enter the value of n+1'th number :10
The first 9 numbers of Fibonacci series are [ 1.  1.  2.  3.  5.  8. 13. 21. 34.] . 

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.