HP Forums

Full Version: (35S) Pell's Equation Programme
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The programme finds the primitive integer solution of Pell’s equation
x^2 – D * y^2 = ± 1
for integer input D and 1 or -1 for solution of the +1 or -1 case respectively.

Code:
1    LBL P
2    STO K
3    1
4    STO F
5    REGZ►A
6    ENTER
7    √x
8    IP
9    STO B
10    STO D
11    STO H
12    x^2
13    -
14    STO I
15    x=0?
16    RTN
17    2
18    RCL* B
19    RCL/ I
20    IP
21    STO C
22    STO G
23    RCL* B
24    1
25    +
26    STO E
27    RCL I
28    1
29    x≠y?
30    GTO P037
31    RCL K
32    x>0?
33    GTO P082
34    RCL A
35    [D,F,-1]
36    RTN
37    0
38    STO J
39    RCL J
40    NOT
41    STO J
42    RCL C
43    RCL* I
44    RCL- H
45    STO H
46    x^2
47    +/-
48    RCL+ A
49    RCL/ I
50    IP
51    STO I
52    1
53    x≠y?
54    GTO P067
55    RCL K
56    x<0?
57    GTO P062
58    RCL J
59    x≠0?
60    GTO P082
61    GTO P067
62    RCL J
63    x=0?
64    GTO P082
65    0
66    RTN
67    RCL B
68    RCL+ H
69    RCL/ I
70    IP
71    STO C
72    RCL* E
73    RCL+ D
74    x<>E
75    STO D
76    RCL C
77    RCL* G
78    RCL+ F
79    x<>G
80    STO F
81    GTO P039
82    RCL A
83    [E,G,K]
84    RTN

For input 13 & 1 the programme returns [649, 180, 1] & indeed
649^2 – 13 * 180^2 = 1
Similarly input 13 & -1 returns [18, 5, -1] &
18^2 – 13 * 5^2 = -1
Input 7 & -1 returns 0, indicating that
x^2 – 7 * y^2 = -1
has no integer solutions for x & y.
Reference URL's