jpp/lab1/zad4/rec/module.adb

76 lines
1.6 KiB
Ada

with Ada.Strings; use Ada.Strings;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package body Module is
function factorial(n : Natural) return Natural is
begin
if (n = 0 or n = 1 ) then
return 1;
end if;
return n * factorial(n - 1);
end factorial;
function gcd(a : Natural; b : Natural) return Natural is
begin
if b = 0 then
return a;
end if;
return gcd(b, a mod b);
end gcd;
function gcdExtended(a : Integer; b : Integer; x : out Integer; y : out Integer) return Integer is
x1 : Integer;
y1 : Integer;
d : Integer;
begin
if b = 0 then
x := 1;
y := 0;
return a;
end if;
d := gcdExtended(b, a rem b, x1, y1);
x := y1;
y := x1 - y1 * (a / b);
return d;
end gcdExtended;
function diophantine(a : Integer; b : Integer; c : Integer) return Result is
r : Result;
x0 : Integer := 0;
y0 : Integer := 0;
g : Integer := gcdExtended(abs a, abs b, x0, y0);
begin
r.n := 0;
r.m := 0;
r.resultExists := False;
if a = 0 and b = 0 then
return r;
end if;
if (c rem g) /= 0 then
return r;
end if;
x0 := x0 * (c / g);
y0 := y0 * (c / g);
if a < 0 then
x0 := -x0;
end if;
if b < 0 then
y0 := -y0;
end if;
r.n := x0;
r.m := y0;
r.resultExists := True;
return r;
end diophantine;
end Module;