Pell's equation: Difference between revisions

Added Wren
(Added Wren)
Line 1,168:
x^2 - 181 * y^2 = 1 for x = 2,469,645,423,824,185,801 and y = 183,567,298,683,461,940
x^2 - 277 * y^2 = 1 for x = 159,150,073,798,980,475,849 and y = 9,562,401,173,878,027,020</pre>
 
=={{header|Wren}}==
{{trans|Sidef}}
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/big" for BigInt
import "/fmt" for Fmt
 
var solvePell = Fn.new { |n|
n = BigInt.new(n)
var x = n.isqrt
var y = x.copy()
var z = BigInt.one
var r = x * 2
var e1 = BigInt.one
var e2 = BigInt.zero
var f1 = BigInt.zero
var f2 = BigInt.one
while (true) {
y = r*z - y
z = (n - y*y) / z
r = (x + y) / z
var t = e1.copy()
e1 = e2.copy()
e2 = r*e2 + t
t = f1.copy()
f1 = f2.copy()
f2 = r*f2 + t
var a = e2 + x*f2
var b = f2.copy()
if (a*a - n*b*b == BigInt.one) return [a, b]
}
}
 
for (n in [61, 109, 181, 277]) {
var res = solvePell.call(n)
Fmt.print("x² - $3dy² = 1 for x = $-21i and y = $i", n, res[0], res[1])
}</lang>
 
{{out}}
<pre>
x² - 61y² = 1 for x = 1766319049 and y = 226153980
x² - 109y² = 1 for x = 158070671986249 and y = 15140424455100
x² - 181y² = 1 for x = 2469645423824185801 and y = 183567298683461940
x² - 277y² = 1 for x = 159150073798980475849 and y = 9562401173878027020
</pre>
 
=={{header|zkl}}==
9,476

edits