真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

python斐波拉函數(shù) python斐波拉契函數(shù)

用python編寫函數(shù)計(jì)算斐波那契數(shù)列的前n項(xiàng),并將結(jié)果存在Fibonacci.txt中,每行5個(gè)

def?Fibonacci(n):

富源ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

if?n?==?1:

return?1

dic?=?[-1?for?i?in?xrange(n)]

dic[0],?dic[1]?=?1,?1

helper(n-1,?dic)

linesize?=?5

file=open('Fibonacci.txt',?'w')

for?loop?in?range(len(dic)/linesize):

line?=?[]

for?i?in?range(linesize):

line.append(dic[i?+?linesize?*?loop])

file.write("\t".join([str(x)?for?x?in?line])?+?"\n")

file.close()

def?helper(n,?dic):

if?dic[n]??0:

dic[n]?=?helper(n-1,?dic)+helper(n-2,?dic)

return?dic[n]

(python編程)使用函數(shù)輸出斐波那契數(shù)列:[-1, -1, -2, -3, -5, -8, -13, -21, -34, -55](添加到列表)?

你看看你遞歸代碼的復(fù)雜度 是O(2^n) 而第二個(gè)的復(fù)雜度是O(n) 運(yùn)行效率當(dāng)然不同COUNTER = 0

def fibn(n):

global COUNTER

COUNTER += 1

if n == 0:

return 1

elif n == 1:

return 1

else:

return fibn(n-1) + fibn(n-2)

statistics = []

for i in range(35):

COUNTER = 0

fibn(i + 1)

statistics.append(((i + 1), COUNTER))

print statistics[(1, 1), (2, 3), (3, 5), (4, 9), (5, 15), (6, 25), (7, 41), (8, 67), (9, 109), (10, 177), (11, 287), (12, 465), (13, 753), (14, 1219), (15, 1973), (16, 3193), (17, 5167), (18, 8361), (19, 13529), (20, 21891), (21, 35421), (22, 57313), (23, 92735), (24, 150049), (25, 242785), (26, 392835), (27, 635621), (28, 1028457), (29, 1664079), (30, 2692537), (31, 4356617), (32, 7049155), (33, 11405773), (34, 18454929), (35, 29860703)]做了一個(gè)簡(jiǎn)單的proflieimport cProfile

import pstats

def fibn(n):

if n == 0:

return 1

elif n == 1:

return 1

else:

return fibn(n-1) + fibn(n-2)

print ' i, calls, time'

for i in range(50):

pr = cProfile.Profile()

pr.enable()

fibn(i)

pr.disable()

stats = pstats.Stats(pr)

stats.strip_dirs()

st = stats.stats[('test1.py', 3, 'fibn')]

print '%3d, %10d, %8f' % (i, st[1], st[3])

i, calls, time 0, 1, 0.000000 1, 1, 0.000001 2, 3, 0.000003 3, 5, 0.000005 4, 9, 0.000008 5, 15, 0.000012 6, 25, 0.000020 7, 41, 0.000033 8, 67, 0.000165 9, 109, 0.000088 10, 177, 0.000141 11, 287, 0.000228 12, 465, 0.000450 13, 753, 0.000601 14, 1219, 0.001016 15, 1973, 0.003561 16, 3193, 0.002593 17, 5167, 0.004372 18, 8361, 0.007097 19, 13529, 0.011073 20, 21891, 0.018552 21, 35421, 0.032467 22, 57313, 0.051762 23, 92735, 0.095383 24, 150049, 0.133490 25, 242785, 0.212390 26, 392835, 0.352861 27, 635621, 0.578204 28, 1028457, 0.987839 29, 1664079, 1.506812 30, 2692537, 2.682802 31, 4356617, 3.998936 32, 7049155, 8.089419 33, 11405773, 13.058235 34, 18454929, 23.930004 35, 29860703, 36.503880目測(cè)fibn(50)要算出來需要兩周

用python函數(shù)寫斐波那契數(shù)列是什么?

斐波那契數(shù)列指的是這樣一個(gè)數(shù)列 0, 1, 1, 2, 3, 5, 8, 13,特別指出:第0項(xiàng)是0,第1項(xiàng)是第一個(gè)1。從第三項(xiàng)開始,每一項(xiàng)都等于前兩項(xiàng)之和。

# 判斷輸入的值是否合法

if nterms = 0:

print("請(qǐng)輸入一個(gè)正整數(shù)。")

elif nterms == 1:

print("斐波那契數(shù)列:")

print(n1)

else:

print("斐波那契數(shù)列:")

print(n1,",",n2,end=" , ")

while count nterms:

nth = n1 + n2

print(nth,end=" , ")

# 更新值

n1 = n2

n2 = nth

count += 1

平方與前后項(xiàng)

從第二項(xiàng)開始(構(gòu)成一個(gè)新數(shù)列,第一項(xiàng)為1,第二項(xiàng)為2,……),每個(gè)偶數(shù)項(xiàng)的平方都比前后兩項(xiàng)之積多1,每個(gè)奇數(shù)項(xiàng)的平方都比前后兩項(xiàng)之積少1。如:第二項(xiàng) 1 的平方比它的前一項(xiàng) 1 和它的后一項(xiàng) 2 的積 2 少 1,第三項(xiàng) 2 的平方比它的前一項(xiàng) 1 和它的后一項(xiàng) 3 的積 3 多 1。


本文題目:python斐波拉函數(shù) python斐波拉契函數(shù)
轉(zhuǎn)載來源:http://weahome.cn/article/doeisdo.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部