Python Fibonacci Generator

前端 未结 17 1341
别那么骄傲
别那么骄傲 2020-11-27 20:59

I need to make a program that asks for the amount of Fibonacci numbers printed and then prints them like 0, 1, 1, 2... but I can\'t get it to work. My code looks the followi

17条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 21:44

    Also you can try the closed form solution (no guarantees for very large values of n due to rounding/overflow errors):

    root5 = pow(5, 0.5)
    ratio = (1 + root5)/2
    
    def fib(n):
        return int((pow(ratio, n) - pow(1 - ratio, n))/root5)
    

提交回复
热议问题