📜  Python| sympy.fibonacci() 方法

📅  最后修改于: 2022-05-13 01:54:51.616000             🧑  作者: Mango

Python| sympy.fibonacci() 方法

借助sympy.fibonacci()方法,我们可以在 SymPy 中找到斐波那契数和斐波那契多项式。

斐波那契(n) -


斐波那契数是由初始项定义的整数序列F_0 = 0 , F_1 = 1和两项递推关系F_n = F_{n-1} + F_{n-2} .

示例 #1:

# 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))  

输出:

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

斐波那契(n, k) -

斐波那契多项式定义为F_1(k) = 1 , F_2(k) = k , 和F_n(k) = k*F_{n-1}(k) + F_{n-2}(k)为了n > 2 .对于所有正整数n , F_n(1) = F_n .

示例 #2:

# 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))  

输出:

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

示例#3:

# 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))  

输出:

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