diff --git a/zad6/.ccls b/zad6/.ccls new file mode 100644 index 0000000..9f91bd0 --- /dev/null +++ b/zad6/.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/zad6/Cargo.lock b/zad6/Cargo.lock new file mode 100644 index 0000000..87a4534 --- /dev/null +++ b/zad6/Cargo.lock @@ -0,0 +1,23 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cc" +version = "1.0.90" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5" + +[[package]] +name = "libc" +version = "0.2.153" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" + +[[package]] +name = "zad6" +version = "0.1.0" +dependencies = [ + "cc", + "libc", +] diff --git a/zad6/Cargo.toml b/zad6/Cargo.toml new file mode 100644 index 0000000..2993348 --- /dev/null +++ b/zad6/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "zad6" +version = "0.1.0" +edition = "2021" + +[dependencies] +libc = "0.2.153" + +[build-dependencies] +cc = "1.0.90" diff --git a/zad6/build.rs b/zad6/build.rs new file mode 100644 index 0000000..f8602ff --- /dev/null +++ b/zad6/build.rs @@ -0,0 +1,8 @@ +extern crate cc; + +fn main() { + cc::Build::new() + .file("src/mod_iter.c") + .shared_flag(true) + .compile("mod.a"); +} diff --git a/zad6/src/main.rs b/zad6/src/main.rs new file mode 100644 index 0000000..aa892fc --- /dev/null +++ b/zad6/src/main.rs @@ -0,0 +1,27 @@ +#[repr(C)] +pub struct Result { + pub resultExists: libc::c_int, + pub n: libc::c_int, + pub m: libc::c_int, +} + +extern "C" { + pub fn gcd(a: libc::c_uint, b: libc::c_uint) -> libc::c_uint; + + pub fn factorial(n: libc::c_uint) -> libc::c_uint; + + pub fn diophantine(a: libc::c_int, b: libc::c_int, c: libc::c_int) -> Result; +} + +fn main() { + unsafe { + println!("gcd(a, b): {}", gcd(10, 5)); + println!("factorial(a): {}", factorial(5)); + let r = diophantine(1027, 712, 1); + if r.resultExists == 0 { + println!("dupa"); + } else { + println!("diophantine(a, b, c): {}, {}", r.n, r.m); + } + } +} diff --git a/zad6/src/mod_iter.c b/zad6/src/mod_iter.c new file mode 100644 index 0000000..ff6a3c3 --- /dev/null +++ b/zad6/src/mod_iter.c @@ -0,0 +1,87 @@ +#include +#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; +} + +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; +} + +typedef struct { + bool resultExists; + int n; + int m; +} Result; + +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/zad6/src/mod_rec.c b/zad6/src/mod_rec.c new file mode 100644 index 0000000..d8ec451 --- /dev/null +++ b/zad6/src/mod_rec.c @@ -0,0 +1,68 @@ +#include +#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); +} + +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; +} + +typedef struct { + bool resultExists; + int n; + int m; +} Result; + +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; +}