Skip to content
Snippets Groups Projects
Commit 350c8106 authored by Jonas's avatar Jonas
Browse files

Refactored and corrected functions and main

parent 3a360615
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
jmaths/bin/main 100644 → 100755
No preview for this file type
/*
* 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
/*
* 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;
}
......@@ -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));
uint64_t a, b, r;
double timeT;
printf("Chose you number : ");
scanf(" %ld %ld", &a, &b);
return 0;
}
clock_t t;
long PPCM(unsigned long n, unsigned long m)
{
int mul_n = n, mul_m = m;
// 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);
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;
}
// 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);
long PPCD(unsigned long n, unsigned long m)
{
long cn = n; // changing n
while((m % cn != 0) || (n % cn != 0))
// PRIME
t = clock();
for (int i = 0; i < 100; i++)
{
cn -= 1;
}
return cn;
printf("%d is %s\n", i, isPrime(i) ? "Prime": "Not Prime");
}
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment