How to write the Fibonacci Sequence?

前端 未结 30 2339
醉酒成梦
醉酒成梦 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:19

    Just going through http://projecteuler.net/problem=2 this was my take on it

    # Even Fibonacci numbers
    # Problem 2
    
    def get_fibonacci(size):
        numbers = [1,2]
        while size > len(numbers):
            next_fibonacci = numbers[-1]+numbers[-2]
            numbers.append(next_fibonacci)
    
        print numbers
    
    get_fibonacci(20)
    

提交回复
热议问题