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

Recommended Answers

All 4 Replies

Hey try this

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

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
;)

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 :)

Wow!

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

Cheers,
ymf

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.