Euler program (python) problem 65

Source: Internet
Author: User

Convergents of Eproblem 65

The square root of 2 can be written as an infinite continued fraction.

√2 = 1 + 1
  2 + 1
    2 + 1
      2 +< /td> 1
        2 + ...

The infinite continued fraction can be written,√2 = [1; (2)] and (2) indicates that 2 repeats ad infinitum. In a similar way,√23 = [4; (1,3,1,8)].

It turns out this sequence of the partial values of continued fractions for square roots provide the best rational approxi Mations. Let us consider the convergents for√2.

1 + 1 = 3/2
2
1 + 1 = 7/5
  2 + 1
    2  
/table>
1 + 1 = 17/12
  2 + 1  
    2 +< /td> 1 & nbsp;
      2  
 
1 + 1 = 41/29
  2 + 1
    2 + 1  
    &nbs P; 2 + 1  
        2

Hence the sequence of the first ten convergents for√2 are:

1, 3/2, 7/5, 17/12, 41/29, 99/70, 239/169, 577/408, 1393/985, 3363/2378, ...

What's most surprising are that the important mathematical constant,
e = [2; 1,2,1, 1,4,1, 1,6,1, ...,K, 1, ...].

The first ten terms in the sequence of convergents for e is:

2, 3, 8/3, 11/4, 19/7, 87/32, 106/39, 193/71, 1264/465, 1457/536, ...

The sum of digits in the numerator of the 10th convergent is 1+4+5+7=17.

Find the sum of digits in the numerator of the 100th convergent of the continued fraction for e.


Python code:

def func (result,i):
T=I*RESULT[1]+RESULT[0]
RESULT[0]=RESULT[1]
Result[1]=t
return result


def func1 (x):
if (x%3) ==0:
k=2* (X/3)
Else
K=1
Return K


n=100
RESULT=[1,FUNC1 (N)]
For I in Range (n-1,1,-1):
Result=func (RESULT,FUNC1 (i))


RESULT[0]+=2*RESULT[1]
TEMP=STR (int (result[0]))
Result=0
K=len (temp)
For I in Range (0,k):
Result+=int (Temp[i])
Print (Result)


Time: <1s

Euler program (python) problem

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.