Pell's equation: Difference between revisions

Content added Content deleted
Line 602: Line 602:
=={{header|langur}}==
=={{header|langur}}==
{{trans|D}}
{{trans|D}}
{{works with|langur|0.10}}
Prior to 0.10, multi-variable declaration/assignment would use parentheses around variable names and values.

<lang langur>val .fun = f [.b, .b x .c + .a]
<lang langur>val .fun = f [.b, .b x .c + .a]


val .solvePell = f(.n) {
val .solvePell = f(.n) {
val .x = truncate .n ^/ 2
val .x = truncate .n ^/ 2
var (.y, .z, .r) = (.x, 1, .x x 2)
var .y, .z, .r = .x, 1, .x x 2
var (.e1, .e2, .f1, .f2) = (1, 0, 0, 1)
var .e1, .e2, .f1, .f2 = 1, 0, 0, 1


for {
for {
Line 613: Line 616:
.z = (.n - .y x .y) \ .z
.z = (.n - .y x .y) \ .z
.r = (.x + .y) \ .z
.r = (.x + .y) \ .z
(.e1, .e2) = .fun(.e1, .e2, .r)
.e1, .e2 = .fun(.e1, .e2, .r)
(.f1, .f2) = .fun(.f1, .f2, .r)
.f1, .f2 = .fun(.f1, .f2, .r)
var (.a, .b) = (.f2, .e2)
var .a, .b = .f2, .e2
(.b, .a) = .fun(.b, .a, .x)
.b, .a = .fun(.b, .a, .x)
if .a^2 - .n x .b^2 == 1: return [.a, .b]
if .a^2 - .n x .b^2 == 1: return [.a, .b]
}
}
Line 622: Line 625:


for .n in [61, 109, 181, 277] {
for .n in [61, 109, 181, 277] {
val (.x, .y) = .solvePell(.n)
val .x, .y = .solvePell(.n)
writeln $".x^2 - \.n:3; x .y^2 = 1 for .x = \.x:27; and .y = \.y:25;"
writeln $".x^2 - \.n:3; x .y^2 = 1 for .x = \.x:27; and .y = \.y:25;"
}</lang>
}</lang>