How to resolve the algorithm Pell's equation step by step in the Delphi programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Pell's equation step by step in the Delphi programming language
Table of Contents
Problem Statement
Pell's equation (also called the Pell–Fermat equation) is a Diophantine equation of the form: with integer solutions for x and y, where n is a given non-square positive integer.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pell's equation step by step in the Delphi programming language
Source code in the delphi programming language
program Pells_equation;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
Velthuis.BigIntegers;
type
TPellResult = record
x, y: BigInteger;
end;
function SolvePell(nn: UInt64): TPellResult;
var
n, x, y, z, r, e1, e2, f1, t, u, a, b: BigInteger;
begin
n := nn;
x := nn;
x := BigInteger.Sqrt(x);
y := BigInteger(x);
z := BigInteger.One;
r := x shl 1;
e1 := BigInteger.One;
e2 := BigInteger.Zero;
f1 := BigInteger.Zero;
b := BigInteger.One;
while True do
begin
y := (r * z) - y;
z := (n - (y * y)) div z;
r := (x + y) div z;
u := BigInteger(e1);
e1 := BigInteger(e2);
e2 := (r * e2) + u;
u := BigInteger(f1);
f1 := BigInteger(b);
b := r * b + u;
a := e2 + x * b;
t := (a * a) - (n * b * b);
if t = 1 then
begin
with Result do
begin
x := BigInteger(a);
y := BigInteger(b);
end;
Break;
end;
end;
end;
const
ns: TArray = [61, 109, 181, 277];
fmt = 'x^2 - %3d*y^2 = 1 for x = %-21s and y = %s';
begin
for var n in ns do
with SolvePell(n) do
writeln(format(fmt, [n, x.ToString, y.ToString]));
{$IFNDEF UNIX} readln; {$ENDIF}
end.
You may also check:How to resolve the algorithm Almkvist-Giullera formula for pi step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Read a file character by character/UTF8 step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the Elixir programming language
You may also check:How to resolve the algorithm Maze generation step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Happy numbers step by step in the Clojure programming language