Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
65
Task/Pells-equation/Ada/pells-equation.adb
Normal file
65
Task/Pells-equation/Ada/pells-equation.adb
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
with Ada.Text_Io;
|
||||
with Ada.Numerics.Elementary_Functions;
|
||||
with Ada.Numerics.Big_Numbers.Big_Integers;
|
||||
|
||||
procedure Pells_Equation is
|
||||
use Ada.Numerics.Big_Numbers.Big_Integers;
|
||||
|
||||
type Pair is
|
||||
record
|
||||
V1, V2 : Big_Integer;
|
||||
end record;
|
||||
|
||||
procedure Solve_Pell (N : Natural; X, Y : out Big_Integer) is
|
||||
use Ada.Numerics.Elementary_Functions;
|
||||
Big_N : constant Big_Integer := To_Big_Integer (N);
|
||||
XX : constant Big_Integer := To_Big_Integer (Natural (Float'Floor (Sqrt (Float (N)))));
|
||||
begin
|
||||
if XX**2 = Big_N then
|
||||
X := 1; Y := 0;
|
||||
return;
|
||||
end if;
|
||||
|
||||
declare
|
||||
YY : Big_Integer := XX;
|
||||
Z : Big_Integer := 1;
|
||||
R : Big_Integer := 2 * XX;
|
||||
E : Pair := Pair'(V1 => 1, V2 => 0);
|
||||
F : Pair := Pair'(V1 => 0, V2 => 1);
|
||||
begin
|
||||
loop
|
||||
YY := R * Z - YY;
|
||||
Z := (Big_N - YY**2) / Z;
|
||||
R := (XX + YY) / Z;
|
||||
E := Pair'(V1 => E.V2, V2 => R * E.V2 + E.V1);
|
||||
F := Pair'(V1 => F.V2, V2 => R * F.V2 + F.V1);
|
||||
X := E.V2 + XX * F.V2;
|
||||
Y := F.V2;
|
||||
exit when X**2 - Big_N * Y**2 = 1;
|
||||
end loop;
|
||||
end;
|
||||
end Solve_Pell;
|
||||
|
||||
procedure Test (N : Natural) is
|
||||
|
||||
package Natural_Io is new Ada.Text_Io.Integer_Io (Natural);
|
||||
use Ada.Text_Io, Natural_Io;
|
||||
|
||||
X, Y : Big_Integer;
|
||||
begin
|
||||
Solve_Pell (N, X => X, Y => Y);
|
||||
Put ("X**2 - ");
|
||||
Put (N, Width => 3);
|
||||
Put (" * Y**2 = 1 for X = ");
|
||||
Put (To_String (X, Width => 22));
|
||||
Put (" and Y = ");
|
||||
Put (To_String (Y, Width => 20));
|
||||
New_Line;
|
||||
end Test;
|
||||
|
||||
begin
|
||||
Test (61);
|
||||
Test (109);
|
||||
Test (181);
|
||||
Test (277);
|
||||
end Pells_Equation;
|
||||
53
Task/Pells-equation/Fortran/pells-equation.f
Normal file
53
Task/Pells-equation/Fortran/pells-equation.f
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
PROGRAM Pell
|
||||
IMPLICIT NONE
|
||||
INTEGER n(4)
|
||||
DATA n /61, 109, 181, 277/
|
||||
INTEGER*16 x, y
|
||||
INTEGER i
|
||||
|
||||
DO 10, i = 1, size(n)
|
||||
CALL solve(n(i), x, y)
|
||||
10 PRINT 100, n(i), x, y
|
||||
|
||||
100 FORMAT ('x² - ', i3, 'y² = 1; x = ', i24, ' and y = ', i24)
|
||||
END PROGRAM
|
||||
|
||||
|
||||
SUBROUTINE solve(n, a, b)
|
||||
IMPLICIT INTEGER*16 (A-Z)
|
||||
INTEGER n
|
||||
|
||||
x = INT(sqrt(REAL(n)))
|
||||
IF (x**2 .ne. n) GOTO 10
|
||||
|
||||
a = 1 ! perfect square; only trivial solution exists
|
||||
b = 0
|
||||
RETURN
|
||||
|
||||
10 y = x
|
||||
z = 1
|
||||
r = 2*x
|
||||
e1 = 1
|
||||
e2 = 0
|
||||
f1 = 0
|
||||
f2 = 1
|
||||
|
||||
20 y = r*z - y
|
||||
z = (n - y**2) / z
|
||||
r = (x + y) / z
|
||||
|
||||
e3 = r*e2 + e1
|
||||
e1 = e2
|
||||
e2 = e3
|
||||
|
||||
f3 = r*f2 + f1
|
||||
f1 = f2
|
||||
f2 = f3
|
||||
|
||||
a = e2 + x*f2
|
||||
b = f2
|
||||
|
||||
IF (a**2 - n*b**2 .eq. 1) RETURN
|
||||
GOTO 20
|
||||
|
||||
END SUBROUTINE
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
val fun = fn a, b, c: [b, b * c + a]
|
||||
val fun = fn(a, b, c) { [b, b * c + a] }
|
||||
|
||||
val solvePell = fn(n) {
|
||||
val x = trunc(n ^/ 2)
|
||||
|
|
@ -17,7 +17,7 @@ val solvePell = fn(n) {
|
|||
}
|
||||
|
||||
val C = fn(x) {
|
||||
# format number string with commas
|
||||
# format integer string with commas
|
||||
var neg, s = "", x -> string
|
||||
if s[1] == '-' {
|
||||
neg, s = "-", less(s, of=1)
|
||||
|
|
|
|||
29
Task/Pells-equation/Pluto/pells-equation.pluto
Normal file
29
Task/Pells-equation/Pluto/pells-equation.pluto
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
require "bignum"
|
||||
local fmt = require "fmt"
|
||||
|
||||
local function solve_pell(n)
|
||||
n = bigint.new(n)
|
||||
local x = bigint.sqrt(n)
|
||||
local y = x
|
||||
local z = bigint.one
|
||||
local r = x * 2
|
||||
local e1 = bigint.one
|
||||
local e2 = bigint.zero
|
||||
local f1 = bigint.zero
|
||||
local f2 = bigint.one
|
||||
while true do
|
||||
y = r * z - y
|
||||
z = (n - y * y) / z
|
||||
r = (x + y) / z
|
||||
e1, e2 = e2, r * e2 + e1
|
||||
f1, f2 = f2, r * f2 + f1
|
||||
local a = e2 + x * f2
|
||||
local b = f2
|
||||
if (a * a - n * b * b == bigint.one) then return {a, b} end
|
||||
end
|
||||
end
|
||||
|
||||
for {61, 109, 181, 277} as n do
|
||||
local [a, b] = solve_pell(n)
|
||||
fmt.print("x² - %3dy² = 1 for x = %-21s and y = %s", n, a, b)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue