diff --git a/jmaths/Makefile b/jmaths/Makefile index 83ef3363c6a2609f456ef17d6d3c0540c0e82a3f..9b22df60832fdc3bc1201f4b6c4022061681890b 100644 --- a/jmaths/Makefile +++ b/jmaths/Makefile @@ -5,7 +5,7 @@ BIN := bin SRC := src INCLUDE := include LIB := lib -LIBRARIES := #Library flags like -lm -lncurses +LIBRARIES := -lm #Library flags like -lm -lncurses EXECUTABLE := main all: $(BIN)/$(EXECUTABLE) diff --git a/jmaths/bin/main b/jmaths/bin/main old mode 100644 new mode 100755 index f01791f353208fc07b4d34bd50d4365ac69d1619..badce77ec9b6ddf0f0d6a3d8c008fb695eb507ae Binary files a/jmaths/bin/main and b/jmaths/bin/main differ diff --git a/jmaths/include/jmath.h b/jmaths/include/jmath.h new file mode 100644 index 0000000000000000000000000000000000000000..a19bfd6ade565c267104e24a3c9c36cc854d1912 --- /dev/null +++ b/jmaths/include/jmath.h @@ -0,0 +1,28 @@ +/* + * HEADER JMATH + * Author : Jonas S. + * Date : 25/10/2021 + ! MATH ALGORITHMS +*/ + +#include <stdio.h> +#include <stdint.h> +#include <stdbool.h> +#include <stdlib.h> +#include <math.h> + +#ifndef _JMATH_H_ +#define _JMATH_H_ + +// MACROS +#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) + + +// DEFINES + +// PROTOTYPE FUNCTIONS +uint64_t GCD(uint64_t a, uint64_t b); +uint64_t LCM(uint64_t a, uint64_t b); +bool isPrime(uint64_t n); + +#endif \ No newline at end of file diff --git a/jmaths/src/jmath.c b/jmaths/src/jmath.c new file mode 100644 index 0000000000000000000000000000000000000000..f368ae734c49a6225f090401fd3a1a4e05ca8556 --- /dev/null +++ b/jmaths/src/jmath.c @@ -0,0 +1,70 @@ +/* + * CODE JMATH + * Author : Jonas S. + * Date : 25/10/2021 + ! MATH ALGORITHMS +*/ + + +#include "../include/jmath.h" +#include <stdio.h> +#include <stdint.h> +#include <stdbool.h> +#include <stdlib.h> +#include <math.h> + + +/** + * @brief Get the Greatest Common Divider + * + * @param a + * @param b + * @return int64_t + * ! Source : https://en.wikipedia.org/wiki/Euclidean_algorithm + */ +uint64_t GCD(uint64_t a, uint64_t b) +{ + int64_t temp; + while(b != 0) + { + temp = b; + b = a % b; + a = temp; + } + return a; +} + +/** + * @brief Get the Lowest Common Multiplier + * + * @param a + * @param b + * @return int64_t + */ +uint64_t LCM(uint64_t a, uint64_t b) +{ + return( a / (GCD(a, b)) * b); +} + + +/** + * @brief Check if a number is prime + * + * @param n Number to check + * @return bool + */ +bool isPrime(uint64_t n) +{ + if(n == 1){return false;} + if(n == 2){return true;} + if(n > 2 && (n % 2 == 0)){return false;} + for (long i = 3; i <= sqrt(n) + 1; i++) + { + if(n % i == 0){return false;} + } + return true; +} + + + + diff --git a/jmaths/src/main.c b/jmaths/src/main.c index 18eaabca2934c547911af3f8bf37e9ccc3c1a1f8..0ba3b8ef17e151f7697da8a68024e43454166ae3 100644 --- a/jmaths/src/main.c +++ b/jmaths/src/main.c @@ -2,73 +2,53 @@ * BASIC MATHS * Author : Jonas S. * Date : 29/09/2021 - ! PPCM, PGDC, + ! TESTING MATH FUNCTIONS */ #include <stdio.h> +#include <stdint.h> +#include <stdbool.h> +#include <time.h> -// DEFINE -#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) - -long PPCM(unsigned long m, unsigned long n); -long PPCM_V2(unsigned long m, unsigned long n); -long PPCD(unsigned long n, unsigned long m); +#include "../include/jmath.h" int main() { - long n, m; - printf("Donnez vos nombre :\n"); - scanf("%lu %lu", &n, &m ); - printf("Le PPCM de %lu et %lu est %lu\n", n, m, PPCM(n, m)); - printf("Le PPCM de %lu et %lu est %lu\n", n, m, PPCM_V2(n, m)); - printf("Le PPCD de %lu et %lu est %lu\n", n, m, PPCD(n, m)); - - - return 0; -} - -long PPCM(unsigned long n, unsigned long m) -{ - int mul_n = n, mul_m = m; - - while(mul_m != mul_n) - { - if (mul_n < mul_m){mul_n += n;} - else if(mul_n > mul_m){mul_m += m;} - } - return mul_n; -} - -long PPCM_V2(unsigned long n, unsigned long m) -{ - int mul_n = n; - while(mul_n % m) - { - mul_n += n; - } - return mul_n; -} - -long PPCD(unsigned long n, unsigned long m) -{ - long cn = n; // changing n - while((m % cn != 0) || (n % cn != 0)) + uint64_t a, b, r; + double timeT; + + printf("Chose you number : "); + scanf(" %ld %ld", &a, &b); + + clock_t t; + + // PGCD + t = clock(); + r = GCD(a, b); + t = clock() - t; + timeT = ((double)t) / CLOCKS_PER_SEC; + printf("COMPUTED PGCD IN %lfs\n", timeT); + printf("PGCD(%ld, %ld) = %ld \n", a, b, r); + + + // PPCM + t = clock(); + r = LCM(a, b); + t = clock() - t; + timeT = ((double)t) / CLOCKS_PER_SEC; + printf("COMPUTED PPCM IN %lfs\n", timeT); + printf("PPCM(%ld, %ld) = %ld \n", a, b, r); + + // PRIME + t = clock(); + for (int i = 0; i < 100; i++) { - cn -= 1; + printf("%d is %s\n", i, isPrime(i) ? "Prime": "Not Prime"); } - return cn; -} + t = clock() - t; + timeT = ((double)t) / CLOCKS_PER_SEC; + printf("COMPUTED 100 NUMBER FOR PRIMABILITY IN %lfms\n", timeT * 1000); -long PPCD_V2(unsigned long n, unsigned long m) -{ - long dividende = n, diviseur = m, quotient, reste; - while(reste != 0) - { - quotient = dividende / diviseur; - reste = dividende % diviseur; - dividende = diviseur; - - } - return diviseur; + return 0; } \ No newline at end of file