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

熱線電話:13121318867

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

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

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

1. 元組實(shí)現(xiàn) 
fibs = [0, 1]
for i in range(8):
 fibs.append(fibs[-2] + fibs[-1])
這能得到一個(gè)在指定范圍內(nèi)的斐波那契數(shù)列的列表。
2. 迭代器實(shí)現(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
這將得到一個(gè)無窮的數(shù)列,可以采用如下方式訪問: 
fibs = Fibs()
for f in fibs:
 if f > 1000:
  print f
  break
 else:
  print f
3. 通過定制類實(shí)現(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")
這樣可以得到一個(gè)類似于序列的數(shù)據(jù)結(jié)構(gòu),可以通過下標(biāo)來訪問數(shù)據(jù):
f = Fib()
print f[0:5]
print f[:10]
4.Python實(shí)現(xiàn)比較簡(jiǎn)易的斐波那契數(shù)列示例
先放一個(gè)斐波那契數(shù)列出來瞧瞧…  
0 1 1 2 3 5 8 13 21 34 55 89 144 233...
首先給頭兩個(gè)變量賦值:   
i, j = 0, 1

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

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

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

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

最后展示運(yùn)行結(jié)果:   

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

總結(jié)

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

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

若不方便掃碼,搜微信號(hào):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(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".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 = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+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); }