实用:Python3计算斐波那契数列前100项分别是多少?

old1=1
old2=1
n = 100
print("(No.1:  %d) "%old1,end='')
for i in range(n-1):
    old1,old2=old2,old1+old2
    print("(No.%d:  %d) "% (i+2,old1),end='')

运行结果:

(No.1:  1) (No.2:  1) (No.3:  2) (No.4:  3) (No.5:  5) (No.6:  8) (No.7:  13) (No.8:  21) (No.9:  34) (No.10:  55) (No.11:  89) (No.12:  144) (No.13:  233) (No.14:  377) (No.15:  610) (No.16:  987) (No.17:  1597) (No.18:  2584) (No.19:  4181) (No.20:  6765) (No.21:  10946) (No.22:  17711) (No.23:  28657) (No.24:  46368) (No.25:  75025) (No.26:  121393) (No.27:  196418) (No.28:  317811) (No.29:  514229) (No.30:  832040) (No.31:  1346269) (No.32:  2178309) (No.33:  3524578) (No.34:  5702887) (No.35:  9227465) (No.36:  14930352) (No.37:  24157817) (No.38:  39088169) (No.39:  63245986) (No.40:  102334155) (No.41:  165580141) (No.42:  267914296) (No.43:  433494437) (No.44:  701408733) (No.45:  1134903170) (No.46:  1836311903) (No.47:  2971215073) (No.48:  4807526976) (No.49:  7778742049) (No.50:  12586269025) (No.51:  20365011074) (No.52:  32951280099) (No.53:  53316291173) (No.54:  86267571272) (No.55:  139583862445) (No.56:  225851433717) (No.57:  365435296162) (No.58:  591286729879) (No.59:  956722026041) (No.60:  1548008755920) (No.61:  2504730781961) (No.62:  4052739537881) (No.63:  6557470319842) (No.64:  10610209857723) (No.65:  17167680177565) (No.66:  27777890035288) (No.67:  44945570212853) (No.68:  72723460248141) (No.69:  117669030460994) (No.70:  190392490709135) (No.71:  308061521170129) (No.72:  498454011879264) (No.73:  806515533049393) (No.74:  1304969544928657) (No.75:  2111485077978050) (No.76:  3416454622906707) (No.77:  5527939700884757) (No.78:  8944394323791464) (No.79:  14472334024676221) (No.80:  23416728348467685) (No.81:  37889062373143906) (No.82:  61305790721611591) (No.83:  99194853094755497) (No.84:  160500643816367088) (No.85:  259695496911122585) (No.86:  420196140727489673) (No.87:  679891637638612258) (No.88:  1100087778366101931) (No.89:  1779979416004714189) (No.90:  2880067194370816120) (No.91:  4660046610375530309) (No.92:  7540113804746346429) (No.93:  12200160415121876738) (No.94:  19740274219868223167) (No.95:  31940434634990099905) (No.96:  51680708854858323072) (No.97:  83621143489848422977) (No.98:  135301852344706746049) (No.99:  218922995834555169026) (No.100:  354224848179261915075)

案例变形:(递归)

def fn(n):
    return fn(n-1)+fn(n-2) if n >1 else n    #看似简单,实际函数的压栈消耗巨大的内存空间且效率低下,不推荐使用

上面案例改进:(递归)

import functools
@functools.lru_cache()
def fn(n):
	return fn(n-1)+fn(n-2) if n >1 else n

fn(100)

运行结果:

354224848179261915075

案例变形:(递归优化)

print(1,end=' ')
def fn(n,old1=0,old2=1):
    old1,old2 = old2,old1+old2
    print(old2,end=' ')
    if n == 2:
        return
    fn(n-1,old1,old2)    #相较for循环,递归有深度限制
    
fn(100)

运行结果:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 2971215073 4807526976 7778742049 12586269025 20365011074 32951280099 53316291173 86267571272 139583862445 225851433717 365435296162 591286729879 956722026041 1548008755920 2504730781961 4052739537881 6557470319842 10610209857723 17167680177565 27777890035288 44945570212853 72723460248141 117669030460994 190392490709135 308061521170129 498454011879264 806515533049393 1304969544928657 2111485077978050 3416454622906707 5527939700884757 8944394323791464 14472334024676221 23416728348467685 37889062373143906 61305790721611591 99194853094755497 160500643816367088 259695496911122585 420196140727489673 679891637638612258 1100087778366101931 1779979416004714189 2880067194370816120 4660046610375530309 7540113804746346429 12200160415121876738 19740274219868223167 31940434634990099905 51680708854858323072 83621143489848422977 135301852344706746049 218922995834555169026 354224848179261915075 

你可能感兴趣的:(python,Python学习记录)