Euler program (python) Problem 64

Source: Internet
Author: User

ODD Period Square Rootsproblem 64

All square roots is periodic when written as continued fractions and can is written in the form:

√ N = a 0 + 1
a1 + 1
a2 + 1
a3 + ...

For example, let us consider√23:

√23 = 4 +√23-4 = 4 + 1 = 4 + 1
1
√23-4
1 + √23–3
7

If We continue we would get the following expansion:

√23 = 4 + 1
1 + 1
3 + 1
1 + 1
8 + ...

The process can be summarised as follows:

a 0 = 4,   1
 =  √23+4
7
 = 1 +  √23-3
7
a 1 = 1,   7
 =  7 (√23+3)
 = 3 +  √23-3
2
a 2 = 3,   2
 =  2 (√23+3)
 = 1 +  √23-4
7
a 3 = 1,   7
 =  7 (√23+4)
7
 = 8 +  √23-4
a 4 = 8,   1
 =  √23+4
7
 = 1 +  √23-3
7
a 5 = 1,   7
 =  7 (√23+3)
 = 3 +  √23-3
2
a 6 = 3,   2
 =  2 (√23+3)
 = 1 +  √23-4
7
a7 = 1, 7
√23-4
= 7 (√23+4)
7
= 8 + √23-4

It can be seen, the sequence is repeating. For conciseness, we use the notation√23 = [4; (1,3,1,8)] and to indicate the block (1,3,1,8) repeats indefinitely.

The first ten continued fraction representations of (irrational) square roots are:

√2=[1; (2)], period=1
√3=[1, (+)], period=2
√5=[2; (4)], period=1
√6=[2; (2,4)], period=2
√7=[2; (1,1,1,4)], period=4
√8=[2; (1,4)], period=2
√10=[3; (6)], period=1
√11=[3; (3,6)], period=2
√12= [3; (2,6)], period=2
√13=[3; (1,1,1,1,6)], period=5

Exactly four continued fractions, for N ≤13, has an odd period.

How many continued fractions for N ≤10000 has an odd period?


Go to the "thread for Problem" in the forum.


Python Code:

Import Math
Sqrt=math.sqrt


def func (x):
Index=0
b=[0.0,0.0]
c=[0.0]
intcoeff={}
fractioncoeff={}
A=int (sqrt (x))
If sqrt (x)-a==0:
return 0
b[0]=1.0
b[1]=-a*1.0
c=1.0
Index+=1
Intcoeff[index]=a
Tempstr=str (b[0]) + ' _ ' +str (b[1]) + ' _ ' +str (c)
Fractioncoeff[tempstr]=index
While 1:
Result=rationalization (b,c,x)
A=result[' a ']
B=result[' B ']
c=result[' C ')
Tempstr=str (b[0]) + ' _ ' +str (b[1]) + ' _ ' +str (c)
K=fractioncoeff.get (TEMPSTR)
If K==none:
Index+=1
Intcoeff[index]=a
Fractioncoeff[tempstr]=index
Else
Return index-k+1
# The denominator contains the irrational number, the molecule is the rational number
def rationalization (b,c,x):
result={}
TC=B[0]*B[0]*X-B[1]*B[1]
B[0]=int (B[0]*C)
B[1]=int (-B[1]*C)
A=int ((b[0]*sqrt (x) +b[1])/tc)
B[1]-=int (A*TC)
K=GCD (GCD (B[0],abs (b[1)), TC)
B[0]/=k
B[1]/=k
Tc/=k
result[' A ']=a
result[' B ']=b
result[' C ']=TC
return result
#求最大公约数
def gcd (m,n):
K=min (M,n)
For I in Range (k,0,-1):
If M%i==0 and n%i==0:
return I
n=10000
Count=0
For I in Range (2,n+1):
K=int (func (i))
If k%2==1:
Count+=1
Print (count)


Time:3s

Euler program (python) Problem 64

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.