How to write the Fibonacci Sequence?

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

    Canonical Python code to print Fibonacci sequence:

    a,b=1,1
    while True:
      print a,
      a,b=b,a+b       # Could also use b=a+b;a=b-a
    

    For the problem "Print the first Fibonacci number greater than 1000 digits long":

    a,b=1,1
    i=1
    while len(str(a))<=1000:
      i=i+1
      a,b=b,a+b
    
    print i,len(str(a)),a
    

提交回复
热议问题