1

Possible Duplicate:
Python Fibonacci Generator

I am trying to construct a function that can append values to an empty list

n1 = 1

n2 = 2

fn = []

I want to add both n1 and n2 together and then send that value to fn.

Then I want to reassign n1 and n2 to the last two values of the sequence.

I then want to be able to stop it after a certain amount of iterations.

I'm basically trying to construct a fibonacci sequence generator without using the function

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

Example:

 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

So if the input was:

 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)
Community
  • 1
  • 1
O.rka
  • 29,847
  • 68
  • 194
  • 309

1 Answers1

1

You can use this

def fib():
    first, second = 0, 1
    while 1:
        yield first
        first, second = second, first + second
JoseP
  • 611
  • 3
  • 6