diff --git a/jmaths/bin/main b/jmaths/bin/main index badce77ec9b6ddf0f0d6a3d8c008fb695eb507ae..ce25a65a07bc049e448f13361d774287129c15f0 100755 Binary files a/jmaths/bin/main and b/jmaths/bin/main differ diff --git a/jmaths/include/jmath.h b/jmaths/include/jmath.h index a19bfd6ade565c267104e24a3c9c36cc854d1912..6304b53756f0f43bf5179a867c270a78b46c0eb0 100644 --- a/jmaths/include/jmath.h +++ b/jmaths/include/jmath.h @@ -24,5 +24,6 @@ uint64_t GCD(uint64_t a, uint64_t b); uint64_t LCM(uint64_t a, uint64_t b); bool isPrime(uint64_t n); +uint64_t fact(uint64_t n); #endif \ No newline at end of file diff --git a/jmaths/src/jmath.c b/jmaths/src/jmath.c index f368ae734c49a6225f090401fd3a1a4e05ca8556..dd832432cb70f67ff57f1b82b9ee06bf50b24f20 100644 --- a/jmaths/src/jmath.c +++ b/jmaths/src/jmath.c @@ -65,6 +65,21 @@ bool isPrime(uint64_t n) return true; } +/** + * @brief Get the factorial of a number + * + * @param n + * @return uint64_t + */ +uint64_t fact(uint64_t n) +{ + uint64_t r = 1; + for (uint64_t i = 2; i <= n; i++) + { + r *= i; + } + return r; +} diff --git a/jmaths/src/main.c b/jmaths/src/main.c index 0ba3b8ef17e151f7697da8a68024e43454166ae3..e41f9e618ca3261ea4a2c874cc801fba8b5183dc 100644 --- a/jmaths/src/main.c +++ b/jmaths/src/main.c @@ -23,6 +23,16 @@ int main() clock_t t; + // PRIME + t = clock(); + for (int i = 0; i < 10; i++) + { + printf("%d is %s\n", i, isPrime(i) ? "Prime": "Not Prime"); + } + t = clock() - t; + timeT = ((double)t) / CLOCKS_PER_SEC; + printf("COMPUTED 10 NUMBERS FOR PRIMABILITY IN %lfms\n", timeT * 1000); + // PGCD t = clock(); r = GCD(a, b); @@ -31,7 +41,6 @@ int main() printf("COMPUTED PGCD IN %lfs\n", timeT); printf("PGCD(%ld, %ld) = %ld \n", a, b, r); - // PPCM t = clock(); r = LCM(a, b); @@ -40,15 +49,13 @@ int main() printf("COMPUTED PPCM IN %lfs\n", timeT); printf("PPCM(%ld, %ld) = %ld \n", a, b, r); - // PRIME + // FACTORIAL t = clock(); - for (int i = 0; i < 100; i++) - { - printf("%d is %s\n", i, isPrime(i) ? "Prime": "Not Prime"); - } + r = fact(a); t = clock() - t; timeT = ((double)t) / CLOCKS_PER_SEC; - printf("COMPUTED 100 NUMBER FOR PRIMABILITY IN %lfms\n", timeT * 1000); + printf("COMPUTED FACTORIAL IN %lfs\n", timeT); + printf("FACT(%ld) = %ld \n", a, r); return 0; } \ No newline at end of file