Python implements the Fibonacci sequence

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.