diff --git a/zad5/.ccls b/zad5/.ccls new file mode 100644 index 0000000..9f91bd0 --- /dev/null +++ b/zad5/.ccls @@ -0,0 +1,21 @@ +gcc +%c -std=c99 +%h +-Iinclude +-Wall +-Wextra +-Wpedantic +-Wstrict-aliasing +-Wfloat-equal +-Wundef +-Wshadow +-Wpointer-arith +-Wcast-align +-Wstrict-prototypes +-Wstrict-overflow=5 +-Wwrite-strings +-Wcast-qual +-Wswitch-default +-Wswitch-enum +-Wconversion +-Wunreachable-code diff --git a/zad5/include/mod.h b/zad5/include/mod.h new file mode 100644 index 0000000..4a827dc --- /dev/null +++ b/zad5/include/mod.h @@ -0,0 +1,19 @@ +#ifndef _JPP_L1_Z5_MOD_H +#define _JPP_L1_Z5_MOD_H + +#include +#include + +unsigned int factorial(unsigned int n); + +unsigned int gcd(unsigned int a, unsigned int b); + +typedef struct { + bool resultExists; + int n; + int m; +} Result; + +Result diophantine(int a, int b, int c); + +#endif // _JPP_L1_Z5_MOD_H diff --git a/zad5/iter/mod.c b/zad5/iter/mod.c new file mode 100644 index 0000000..79e367b --- /dev/null +++ b/zad5/iter/mod.c @@ -0,0 +1,82 @@ +#include "../include/mod.h" + +#include +#include + +unsigned int factorial(unsigned int n) { + unsigned int result = 1; + + for (size_t i = 1; i <= n; ++i) { + result *= i; + } + + return result; +} + +unsigned int gcd(unsigned int a, unsigned int b) { + if (a == 0) return b; + + size_t r; + + while (b > 0) { + r = a % b; + + a = b; + b = r; + } + + return a; +} + +inline int extended_gcd(int a, int b, int *x, int *y) { + *x = 1; + *y = 0; + int x1 = 0; + int y1 = 1; + int q; + int temp_x, temp_y, temp_b; + while (b != 0) { + q = a / b; + + temp_x = *x - q * x1; + temp_y = *y - q * y1; + *x = x1; + *y = y1; + x1 = temp_x; + y1 = temp_y; + + temp_b = a - q * b; + a = b; + b = temp_b; + } + + return a; +} + +Result diophantine(int a, int b, int c) { + Result r = { + .resultExists = false, + .n = 0, + .m = 0, + }; + + if (a == 0 && b == 0) + return r; + int x0 = 0; + int y0 = 0; + int g = extended_gcd(abs(a), abs(b), &x0, &y0); + + if (c % g) + return r; + + x0 *= c / g; + y0 *= c / g; + if (a < 0) x0 = -x0; + if (b < 0) y0 = -y0; + + r.resultExists = true; + r.n = x0; + r.m = y0; + + return r; +} diff --git a/zad5/rec/mod.c b/zad5/rec/mod.c new file mode 100644 index 0000000..7fe5bdd --- /dev/null +++ b/zad5/rec/mod.c @@ -0,0 +1,63 @@ +#include "../include/mod.h" + +#include +#include +#include +#include + +unsigned int factorial(unsigned int n) { + if (n == 0 || n == 1) { + return 1; + } + + return n * factorial(n - 1); +} + +unsigned int gcd(unsigned int a, unsigned int b) { + if (b == 0) { + return a; + } + + return gcd(b, a % b); +} + +inline int extended_gcd(int a, int b, int *x, int *y) { + if (b == 0) { + *x = 1; + *y = 0; + return a; + } + int x1, y1; + int d = extended_gcd(b, a % b, &x1, &y1); + *x = y1; + *y = x1 - y1 * (a / b); + return d; +} + +Result diophantine(int a, int b, int c) { + Result r = { + .resultExists = false, + .n = 0, + .m = 0, + }; + + if (a == 0 && b == 0) + return r; + int x0 = 0; + int y0 = 0; + int g = extended_gcd(abs(a), abs(b), &x0, &y0); + + if (c % g) + return r; + + x0 *= c / g; + y0 *= c / g; + if (a < 0) x0 = -x0; + if (b < 0) y0 = -y0; + + r.resultExists = true; + r.n = x0; + r.m = y0; + + return r; +} diff --git a/zad5/src/main.adb b/zad5/src/main.adb new file mode 100644 index 0000000..f845744 --- /dev/null +++ b/zad5/src/main.adb @@ -0,0 +1,48 @@ +with Ada.Text_IO; use Ada.Text_IO; +with Ada.Strings; use Ada.Strings; +with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; +with Interfaces.C; use Interfaces.C; + +procedure Main is + + function gcd(a : Natural; b : Natural) return Natural + with + Import => True, + Convention => C, + External_Name => "gcd"; + + function factorial(n : Natural) return Natural + with + Import => True, + Convention => C, + External_Name => "factorial"; + + type Result is record + resultExists : Boolean; + n : Integer; + m : Integer; + end record + with Convention => C; + + function diophantine(a : Integer; b : Integer; c : Integer) return Result + with + Import => True, + Convention => C, + External_Name => "diophantine"; + + 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/zad5/zad5.gpr b/zad5/zad5.gpr new file mode 100644 index 0000000..35a6258 --- /dev/null +++ b/zad5/zad5.gpr @@ -0,0 +1,11 @@ +project Zad5 is + type Impl_Type is ("iter", "rec"); + Impl : Impl_Type := external ("impl", "iter"); + + for Languages use ("ada", "c"); + + for Main use ("main.adb"); + for Source_Dirs use ("src", Impl); + for Object_Dir use "target"; + +end Zad5;