How to write the Fibonacci Sequence?

前端 未结 30 2975
醉酒成梦
醉酒成梦 2020-11-22 00:32

I had originally coded the program wrongly. Instead of returning the Fibonacci numbers between a range (ie. startNumber 1, endNumber 20 should = only those numbers between 1

30条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-22 01:13

    OK.. after being tired of referring all lengthy answers, now find the below sort & sweet, pretty straight forward way for implementing Fibonacci in python. You can enhance it it the way you want by getting an argument or getting user input…or change the limits from 10000. As you need……

    def fibonacci():
        start = 0 
        i = 1 
        lt = []
        lt.append(start)
        while start < 10000:
            start += i
            lt.append(start)
            i = sum(lt[-2:])
            lt.append(i)
        print "The Fibonaccii series: ", lt
    

    This approach also performs good. Find the run analytics below

    In [10]: %timeit fibonacci
    10000000 loops, best of 3: 26.3 ns per loop
    

提交回复
热议问题