The inventor of the Fibonacci sequence was the Italian mathematician Leonardo Fibonacci (Leonardo Fibonacci). The Fibonacci sequence is also known as the Golden segment, or the rabbit sequence. It refers to a sequence of numbers: 0 1 1 2 3 5 8 13 21 34 .... Mathematically, the Fibonacci sequence is defined as a recursive method: F (0) =0,f (1) =1,f (n) =f (n-1) +f (N-2 (N>=2,n belongs to N). The rule of the Fibonacci sequence is simple: the 1th number is 0, the 2nd number is 1, and then each value is the first two bits.
#!/usr/bin/python3# coding=utf-8import timedef fbis (num): result = [0,1] for I in range (num-2): Result.append (Result[-2]+result[-1]) return Resultdef main (): result = Fbis (ten) Fobj = open ("/root/result.txt", "w+") for I , num in enumerate (result): #enumerate可以生成带索引的迭代序列. Print U "%d number is:%d"% (i,num) fobj.write ("%d"%num) time.sleep (1) if __name__ = = ' __main__ ': Main ()
Python implements the Fibonacci sequence