借助於sympy.lucas()方法,我們可以在SymPy中找到盧卡斯數。
lucas(n)-
盧卡斯數滿足與斐波那契數列相似的遞歸關係,其中每個項是前兩個項的和。通過選擇初始值生成它們和
和遞歸關係
。
用法: lucas(n)
參數:
n -它表示要計算其色號的數字。
返回:返回nth盧卡數。
範例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]
相關用法
- Python os.dup()用法及代碼示例
- Python set()用法及代碼示例
- Python next()用法及代碼示例
- Python os.get_blocking()用法及代碼示例
- Python sympy.apart()用法及代碼示例
- Python sympy.nC()用法及代碼示例
- Python os.set_blocking()用法及代碼示例
- Python os.getcwd()用法及代碼示例
- Python os.access()用法及代碼示例
- Python dict pop()用法及代碼示例
- Python sympy.Add()用法及代碼示例
- Python sympy.nP()用法及代碼示例
注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 Python | sympy.lucas() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。