-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpell.py
More file actions
48 lines (34 loc) · 866 Bytes
/
pell.py
File metadata and controls
48 lines (34 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import math
def getContinuedFractionOfSquareRoot(val):
m = [0]
d = [1]
a = [int(math.sqrt(val))]
yield a[0]
while True:
mm = d[-1]*a[-1] - m[-1]
dd = (val - mm*mm) / d[-1]
aa= int( (a[0] + mm)/dd )
m.append(mm)
d.append(dd)
a.append(aa)
yield aa
def checkPellSolution(x,y,n):
return x*x - n*y*y == 1
def solve(D):
if int(math.sqrt(D))**2 == D:
return
continuedFractionGenerator = getContinuedFractionOfSquareRoot(D)
a = continuedFractionGenerator.next()
p = [1, a]
q = [0, 1]
for a in continuedFractionGenerator:
if checkPellSolution(p[-1], q[-1], D):
return (p[-1], q[-1])
p.append(a*p[-1] + p[-2])
q.append(a*q[-1] + q[-2])
for D in xrange(1,1001):
result = solve(D)
if result is None:
print 'No solution for D= %d' % D
continue
print 'Solution for D= %d: x= %d, y=%d' % (D, result[0], result[1])