Python| sympy.fibonacci() 方法
借助sympy.fibonacci()方法,我们可以在 SymPy 中找到斐波那契数和斐波那契多项式。
斐波那契(n) -
斐波那契数是由初始项定义的整数序列 , 和两项递推关系 .
Syntax: fibonacci(n)
Parameter:
n – It denotes the number upto which Fibonacci number is to be calculated.
Returns: Returns the nth Fibonacci number.
示例 #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) -
斐波那契多项式定义为 , , 和为了 .对于所有正整数 , .
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)
示例 #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