Python| sympy.lucas() 方法
借助sympy.lucas()方法,我们可以在 SymPy 中找到卢卡斯数。
卢卡斯(n) -
卢卡斯数满足类似于斐波那契数列的递推关系,其中每一项都是前两项之和。它们是通过选择初始值生成的
![由 QuickLaTeX.com 渲染 L_0 = 2](https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/Python_%7C_sympy.lucas()_method_0.png)
![由 QuickLaTeX.com 渲染 L_1 = 1](https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/Python_%7C_sympy.lucas()_method_1.png)
![由 QuickLaTeX.com 渲染 L_n = L_{n-1} + L_{n-2}](https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/Python_%7C_sympy.lucas()_method_2.png)
Syntax: lucas(n)
Parameter:
n – It denotes the number upto which lucus number is to be calculated.
Returns: Returns the nth lucas number.
示例 #1:
# import sympy
from sympy import *
n = 7
print("Value of n = {}".format(n))
# Use sympy.lucas() method
nth_lucas = lucas(n)
print("Value of nth lucas number : {}".format(nth_lucas))
输出:
Value of n = 7
Value of nth lucas number : 29
示例 #2:
# import sympy
from sympy import *
n = 10
print("Value of n = {}".format(n))
# Use sympy.lucas() method
n_lucas = [lucas(x) for x in range(11)]
print("N lucas number are : {}".format(n_lucas))
输出:
Value of n = 10
N lucas number are : [2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123]