1

重複の可能性:
Pythonフィボナッチジェネレーター

空のリストに値を追加できる関数を作成しようとしています

n1 = 1

n2 = 2

fn = []

n1とn2の両方を足し合わせて、その値をfnに送信したいと思います。

次に、n1とn2をシーケンスの最後の2つの値に再割り当てします。

次に、一定の反復回数の後でそれを停止できるようにしたいと思います。

私は基本的に関数を使用せずにフィボナッチ数列ジェネレーターを構築しようとしています

#s(n) = (1.618^n-(1-1.618)^n)/(5^.5)`

例:

 fn = []




 def fibb(n1,n2,f_iter):
 # n1 would be the first number of the sequence
 # n2 would be the second number of the sequence
 # f_iter would be how many iterations it would do until finished

したがって、入力が次の場合:

 def fibb(1,2,10):  
    return fn

 fn = [1,2,3,5,8,13,21,34,55,89,144,233]

#f_iter(0:1+2=3,1:2+3=5,2:3+5=8,3:5+8=13, . . . 10:89+144=233)
4

1 に答える 1

1

これを使用できます

def fib():
    first, second = 0, 1
    while 1:
        yield first
        first, second = second, first + second
于 2012-12-02T06:20:15.970 に答える