Python computes the Fibonacci sequence

Source: Internet
Author: User


Use Python to calculate the sum of the first 1 million-digit Fibonacci numbers and the result is 4501552.


The following is the code I used, not the middle need some manual action to speed up convergence, interested readers can write code to speed up convergence

To do this first, you can roughly determine the position of the Fibonacci sequence where the 1 million numbers are located.

I=1j=1k=i+jcount=3while count<4850000:    i=j    j=k    k=i+j    count+=1result=str (k) print (' k length ') k_len= Len (Result) print (K_len) sum=0for item in range (0,len (Result)):    Sum+=int (Result[item]) print (' K-count sum ') print (sum ) File_object = open (' Python.txt ', ' W ') list=[str (i) + ' \ n ', str (j) + ' \ n ', str (k) + ' \ n ', str (k_len) + ' \ n ']file_ Object.writelines (list) File_object.close ()
The next step is to determine whether to forward or backward based on the results of the previous

The forward code is

input = open (' Python.txt ') i=input.readline () I=i.strip (' \ n ') i=int (i) J=input.readline () J=j.strip (' \ n ') J=int (j) k= Input.readline () K=k.strip (' \ n ') k=int (k) print (' Read Data end ') input.close () count=0while count<4000:    i=j    j=k    k=i+j    count+=1    result=str (k) Print (' K-length ') k_len=len (result) print (K_len) file_object = open (' Python.txt ', ' W ') list=[str (i) + ' \ n ', str (j) + ' \ n ', str (k) + ' \ n ', str (k_len) + ' \ n ']file_object.writelines (list) File_ Object.close () print (' Iterator up End ')
The backward code is

input = open (' Python.txt ') i=input.readline () I=i.strip (' \ n ') i=int (i) J=input.readline () J=j.strip (' \ n ') J=int (j) k= Input.readline () K=k.strip (' \ n ') k=int (k) print (' Read Data end ') input.close () count=0while count<10:    k=j    J =i    i=k-j    count+=1    result=str (k) Print (' K-length ') k_len=len (result) print (K_len) file_object = open (' Python.txt ', ' W ') list=[str (i) + ' \ n ', str (j) + ' \ n ', str (k) + ' \ n ', str (k_len) + ' \ n ']file_object.writelines (list) File_ Object.close () print (' Iterator down ')
For the convenience of viewing the results there is also a code to view the TXT content

input = open (' Python.txt ') i=input.readline () I=i.strip (' \ n ') print (' I's length ') print (len (i)) j=input.readline () J=j.strip (' \ n ') print (' J's length ') Print (Len (j)) K=input.readline () K=k.strip (' \ n ') print (' k's length ') Print (Len (k)) k_len= Input.readline () K_len=k_len.strip (' \ n ') k_len=int (k_len) print (' Data k length ') print (K_len) result=jsum=0for item in range (0 , Len (Result):    sum+=int (Result[item]) print (' Everyone's sum ') print (sum)
Problems encountered in the experimental process

The key to this problem is that if you judge the number of k digits after each k update, the program becomes a special card, however, if you just perform the update K operation, the program is very efficient, so you need to reduce the K-digit calculation. Then the problem is to determine the position of the 1 millionth Fibonacci sequence, using the idea of dichotomy. According to the number of digits of the feedback of each step, until the k that satisfies the requirement is found.

Another problem with this experiment is that because the i,j,k is a large number of bits, the final reading and writing of TXT becomes the main factor affecting efficiency. Because Python is not a special understanding, do not know that can be like MATLAB, the data will not be placed in txt, and in memory, can be directly called, the program will be more efficient.

Python computes 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.