99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀python實現(xiàn)斐波那契數(shù)列的方法示例
python實現(xiàn)斐波那契數(shù)列的方法示例
2018-01-20
收藏

python實現(xiàn)斐波那契數(shù)列的方法示例

斐波那契數(shù)列,又稱黃金分割數(shù)列,指的是這樣一個數(shù)列:0、1、1、2、3、5、8、13、21、……在數(shù)學上,斐波納契數(shù)列以如下遞歸的方法定義:
F(0)=0,F(xiàn)(1)=1,F(xiàn)(n)=F(n-1)+F(n-2)(n≥2,n∈N*) 。

1. 元組實現(xiàn) 
fibs = [0, 1]
for i in range(8):
 fibs.append(fibs[-2] + fibs[-1])
這能得到一個在指定范圍內(nèi)的斐波那契數(shù)列的列表。
2. 迭代器實現(xiàn)   
class Fibs:
 def __init__(self):
  self.a = 0
  self.b = 1
 
 def next(self):
  self.a, self.b = self.b, self.a + self.b
  return self.a
 
 def __iter__(self):
  return self
這將得到一個無窮的數(shù)列,可以采用如下方式訪問: 
fibs = Fibs()
for f in fibs:
 if f > 1000:
  print f
  break
 else:
  print f
3. 通過定制類實現(xiàn)
class Fib(object):
 def __getitem__(self, n):
  if isinstance(n, int):
   a, b = 1, 1
   for x in range(n):
    a, b = b, a + b
   return a
  elif isinstance(n, slice):
   start = n.start
   stop = n.stop
   a, b = 1, 1
   L = []
   for x in range(stop):
    if x >= start:
     L.append(a)
    a, b = b, a + b
   return L
  else:
   raise TypeError("Fib indices must be integers")
這樣可以得到一個類似于序列的數(shù)據(jù)結構,可以通過下標來訪問數(shù)據(jù):
f = Fib()
print f[0:5]
print f[:10]
4.Python實現(xiàn)比較簡易的斐波那契數(shù)列示例
先放一個斐波那契數(shù)列出來瞧瞧…  
0 1 1 2 3 5 8 13 21 34 55 89 144 233...
首先給頭兩個變量賦值:   
i, j = 0, 1

當然也可以這樣寫:   
i = 0
j = 1
接著定個范圍,就10000之內(nèi)好了:
while i < 10000:
然后在while語句中輸出i并設計邏輯:  
print i,
i, j = j, i+j

在這里需要注意:“i, j = i, i+j”這條代碼不能寫成如下所示: 
i = j
j = i+j

如果寫成這樣,j就不是前兩位相加的值,而是已經(jīng)被j賦過值的i和j相加的值,這樣的話輸出的數(shù)列會如下所示:

0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192

正確的整片代碼如下所示: 
i, j = 0, 1
while i < 10000:
 print i,
 i, j = j, i+j

最后展示運行結果:   

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

總結

以上就是關于利用Python實現(xiàn)斐波那契數(shù)列的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }