Python | sympy.fibonacci() method

With the help of sympy.fibonacci() method, we can find the Fibonacci number and Fibonacci polynomial in SymPy.

fibonacci(n) -
The Fibonacci numbers are the integer sequence defined by the initial terms F_0 = 0, F_1 = 1 and the two-term recurrence relation F_n = F_{n-1} + F_{n-2}.



Syntax: fibonacci(n)

Parameter:
n – It denotes the number upto which Fibonacci number is to be calculated.

Returns: Returns the nth Fibonacci number.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# import sympy 
from sympy import * 
  
n = 7
print("Value of n = {}".format(n))
   
# Use sympy.fibonacci() method 
nth_fibonacci = fibonacci(n)  
      
print("Value of nth fibonacci number : {}".format(nth_fibonacci))  

chevron_right


Output:

Value of n = 7
Value of nth fibonacci number : 13

fibonacci(n, k) -

The Fibonacci polynomials are defined by F_1(k) = 1, F_2(k) = k, and F_n(k) = k*F_{n-1}(k) + F_{n-2}(k) for n > 2. For all positive integers n, F_n(1) = F_n.

Syntax: fibonacci(n, k)

Parameter:
n – It denotes the nth Fibonacci polynomial.
k – It denotes the variable in the Fibonacci polynomial.


Returns: Returns the the nth Fibonacci polynomial in k, Fn(k)

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# import sympy 
from sympy import * 
  
n = 5
k = symbols('x')
print("Value of n = {} and k = {}".format(n, k))
   
# Use sympy.fibonacci() method 
nth_fibonacci_poly = fibonacci(n, k)  
      
print("The nth fibonacci polynomial : {}".format(nth_fibonacci_poly))  

chevron_right


Output:

Value of n = 5 and k = x
The nth fibonacci polynomial : x**4 + 3*x**2 + 1

Example #3:

filter_none

edit
close

play_arrow

link
brightness_4
code

# import sympy 
from sympy import * 
  
n = 6
k = 3
print("Value of n = {} and k = {}".format(n, k))
   
# Use sympy.fibonacci() method 
nth_fibonacci_poly = fibonacci(n, k)  
      
print("The nth fibonacci polynomial value : {}".format(nth_fibonacci_poly))  

chevron_right


Output:

Value of n = 6 and k = 3
The nth fibonacci polynomial value : 360


My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

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.