Jump to content

Pell's equation: Difference between revisions

→‎{{header|Python}}: It's far more Pythonic to use multiple assignment than a helper method which uses arrays for pass-by-reference
(Added Wren)
(→‎{{header|Python}}: It's far more Pythonic to use multiple assignment than a helper method which uses arrays for pass-by-reference)
Line 876:
<lang python>import math
 
def funsolvePell(a, b, cn):
t = a[0]
a[0] = b[0]
b[0] = b[0] * c + t
 
def solvePell(n, a, b):
x = int(math.sqrt(n))
y, z, r = x, 1, x << 1
ze1, e2 = 1, 0
rf1, =f2 x= <<0, 1
e1 = [1]
e2 = [0]
f1 = [0]
f2 = [1]
while True:
y = r * z - y
z = ((n - y * y) // z)
r = (x + y) // z
fun(e1, e2, r)
fun(f1, f2, r)
a[0] = f2[0]
b[0] = e2[0]
fun(b, a, x)
if a[0] * a[0] - n * b[0] * b[0] == 1:
return
 
fun(e1, e2 = e2, e1 + e2 * r)
x = [0]
fun(f1, f2 = f2, f1 + f2 * r)
y = [0]
 
a[0], b = f2[0] * x + e2, f2
if a[0] * a[0] - n * b[0] * b[0] == 1:
fun(b, return a, x)b
 
for n in [61, 109, 181, 277]:
solvePell(n, x, y = solvePell(n)
print("x^2 - %3d * y^2 = 1 for x = %27d and y = %25d" % (n, x[0], y[0]))</lang>
{{out}}
<pre>x^2 - 61 * y^2 = 1 for x = 1766319049 and y = 226153980
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.