0

Hi all,

I tried to write in a fibonacci sequence using python:

unum = int(raw_input('Enter Limit Number to run Fibonacci sequence: '))
numlist = [1,1]
for i in range(unum):
    fib2 = numlist[i+1] + numlist[i]
    numlist.append(fib2)
print "Here's Fibonacci!"
for w in numlist:
    print w,

This works fine. But is there a way to print out the sequence without having to store the first two numbers in the 'numlist' ? i.e generate them within the loop?

-ymf

3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by ymf
0

Hey try this

def f():
  r=0
  p=1
  for i in range(20) :
    print r
    r=r+p
    p=r-p
f()
0

You could do it like this.

how_many_fibs = 100
x,y=0,1
while how_many_fibs:
    x,y=y,y+x
    print y
    how_many_fibs-=1

This will make 100 fibonacci numbers
how this helps
;)

0

U can use the raw_input to get the limit and substitute in the place of 20.
note that 20 numbers in the series are generated and not till 20 ,the last number would be 4181
so design your code to get it the way u want it to be :)

0

Wow!

All your codes are much more simpler and efficient than mine. Thanks a lot :)

Cheers,
ymf

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.