Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | sympy.tribonacci() method

  • Last Updated : 14 Jul, 2019

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

 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. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

tribonacci(n) -


The Tribonacci numbers are the integer sequence defined by the initial terms T_0 = 0, T_1 = 1, T_2 = 1 and the three-term recurrence relation T_n = T_{n-1} + T_{n-2} + T_{n-3}.



Syntax: tribonacci(n)

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

Returns: Returns the nth Tribonacci number.

Example #1:




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

Output:

Value of n = 7
Value of nth tribonacci number : 24

tribonacci(n, k) -

The Tribonacci polynomials are defined by T_0(k) = 0, T_1(k) = 1, T_2(k) = k^2 and T_n(k) = k^2 T_{n-1}(k) + k T_{n-2}(k) + T_{n-3}(k) for n > 2. For all positive integers n, T_n(1) = T_n.



Syntax: tribonacci(n, k)

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

Returns: Returns the nth Tribonacci polynomial in k, Tn(k)

Example #2:




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

Output:

Value of n = 5 and k = x
The nth tribonacci polynomial : x**8 + 3*x**5 + 3*x**2

Example #3:




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

Output:

Value of n = 6 and k = 3
The nth tribonacci polynomial value : 68289



My Personal Notes arrow_drop_up
Recommended Articles
Page :