當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python sympy.lucas()用法及代碼示例

借助於sympy.lucas()方法,我們可以在SymPy中找到盧卡斯數。

lucas(n)-


盧卡斯數滿足與斐波那契數列相似的遞歸關係,其中每個項是前兩個項的和。通過選擇初始值生成它們L_0 = 2L_1 = 1和遞歸關係L_n = L_{n-1} + L_{n-2}


用法: 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]


相關用法


注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 Python | sympy.lucas() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。