根據序列:2/1,3/2,5/3,...生成前30項打印出並求和

根據斐波那契數列

def fab(max): 
    def fib_loop_while(max):
        max = max
        a, b = 0, 1
        while max > 0:
            a, b = b, a + b
            max -= 1
            yield a
    
    son = []
    for i in fib_loop_while(max+2):
        son.append(i)
    son=son[2:]
    
    mother = []
    for i in fib_loop_while(max+1):
        mother.append(i)
    mother=mother[1:]
    
    from fractions import Fraction
    sum_result = 0
    for i, j in zip(son, mother):
        sum_result += Fraction(i, j)
        print(Fraction(i, j))
    return sum_result
print("該數列的前30項和: ",fab(30))
2
3/2
5/3
8/5
13/8
21/13
34/21
55/34
89/55
144/89
233/144
377/233
610/377
987/610
1597/987
2584/1597
4181/2584
6765/4181
10946/6765
17711/10946
28657/17711
46368/28657
75025/46368
121393/75025
196418/121393
317811/196418
514229/317811
832040/514229
1346269/832040
2178309/1346269
該數列的前30項和:  1630465823281650152539544792281534743164959378613014674996887883/33383410530204368141447032661055706206034629863580261272074400
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章