diff --git a/zad2/.gitignore b/zad2/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/zad2/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/zad2/src/iter/module.adb b/zad2/src/iter/module.adb new file mode 100644 index 0000000..07f9af2 --- /dev/null +++ b/zad2/src/iter/module.adb @@ -0,0 +1,95 @@ +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 + result : Natural := 1; + begin + for i in 1 .. n loop + result := result * i; + end loop; + + return result; + end factorial; + + function gcd(a : Natural; b : Natural) return Natural is + r : Natural; + aa : Natural := a; + bb : Natural := b; + begin + if a = 0 then + return b; + end if; + + while bb > 0 loop + r := aa mod bb; + + aa := bb; + bb := r; + end loop; + + return aa; + end gcd; + + function gcdExtended(a : Integer; b : Integer; x : out Integer; y : out Integer) return Integer is + x1 : Integer := 0; + y1 : Integer := 1; + q : Integer; + tempX, tempY, tempB : Integer; + aa : Integer := a; + bb : Integer := b; + begin + x := 1; + y := 0; + while bb /= 0 loop + q := aa / bb; + tempX := x - q * x1; + tempY := y - q * y1; + x := x1; + y := y1; + x1 := tempX; + y1 := tempY; + + tempB := aa - q * bb; + aa := bb; + bb := tempB; + end loop; + + return aa; + 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; diff --git a/zad2/src/main.adb b/zad2/src/main.adb new file mode 100644 index 0000000..d9ea9b3 --- /dev/null +++ b/zad2/src/main.adb @@ -0,0 +1,24 @@ +with Ada.Text_IO; use Ada.Text_IO; +with Ada.Strings; use Ada.Strings; +with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; +with Module; + +procedure Main is + use Module; + + gcdResult : Natural; + factorialResult : Natural; + diophantineResult : Result; +begin + gcdResult := gcd(10, 5); + factorialResult := factorial(5); + diophantineResult := diophantine(1027, 712, 1); + + Put_Line("gcd(a, b):" & Natural'Image(gcdResult)); + Put_Line("factorial(a):" & Natural'Image(factorialResult)); + if not diophantineResult.resultExists then + Put_Line("dupa"); + else + Put_Line("diophantine(a, b, c): " & Integer'Image(diophantineResult.n) & "," & Integer'Image(diophantineResult.m)); + end if; +end Main; diff --git a/zad2/src/module.adb b/zad2/src/module.adb new file mode 100644 index 0000000..a9eea03 --- /dev/null +++ b/zad2/src/module.adb @@ -0,0 +1,76 @@ +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; diff --git a/zad2/src/module.ads b/zad2/src/module.ads new file mode 100644 index 0000000..e78146d --- /dev/null +++ b/zad2/src/module.ads @@ -0,0 +1,20 @@ +with Ada.Strings; use Ada.Strings; +with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; + +package Module is + + type Result is record + resultExists : Boolean; + n : Integer; + m : Integer; + end record; + + function factorial(n : Natural) return Natural; + + function gcd(a : Natural; b : Natural) return Natural; + + function gcdExtended(a : Integer; b : Integer; x : out Integer; y : out Integer) return Integer; + + function diophantine(a : Integer; b : Integer; c : Integer) return Result; + +end Module; diff --git a/zad2/src/rec/module.adb b/zad2/src/rec/module.adb new file mode 100644 index 0000000..a9eea03 --- /dev/null +++ b/zad2/src/rec/module.adb @@ -0,0 +1,76 @@ +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; diff --git a/zad2/zad2.gpr b/zad2/zad2.gpr new file mode 100644 index 0000000..fc89598 --- /dev/null +++ b/zad2/zad2.gpr @@ -0,0 +1,9 @@ +project Zad2 is + type Impl_Type is ("iter", "rec"); + Impl : Impl_Type := external ("impl", "iter"); + + for Main use ("main"); + for Source_Dirs use ("src", "src/" & Impl); + for Object_Dir use "target"; + +end Zad2;