天天看点

计算斐波那契数列前N项(N>=2)之和S(S=T0+T1+...Tn)的算法

1 def number(num):
 2     a=0
 3     b=1
 4     i=2
 5     s=1
 6     if num==2:
 7         return 1
 8     else:
 9         while 1:
10             if i<num:
11                 a, b = b, a+b
12                 s = s+b
13                 i = i+1
14             else:
15                 break
16         return s
17 
18 if __name__ == '__main__':
19     num = input("请输入N")
20     if num.isdigit() and int(num)>=2:
21         res = number(int(num))
22         print(res)
23     else:
24         print("输入有误")