Skip to content
Snippets Groups Projects
Commit 34d6c0f9 authored by Jonas's avatar Jonas
Browse files

Added factorial

parent 350c8106
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -24,5 +24,6 @@ ...@@ -24,5 +24,6 @@
uint64_t GCD(uint64_t a, uint64_t b); uint64_t GCD(uint64_t a, uint64_t b);
uint64_t LCM(uint64_t a, uint64_t b); uint64_t LCM(uint64_t a, uint64_t b);
bool isPrime(uint64_t n); bool isPrime(uint64_t n);
uint64_t fact(uint64_t n);
#endif #endif
\ No newline at end of file
...@@ -65,6 +65,21 @@ bool isPrime(uint64_t n) ...@@ -65,6 +65,21 @@ bool isPrime(uint64_t n)
return true; 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;
}
...@@ -23,6 +23,16 @@ int main() ...@@ -23,6 +23,16 @@ int main()
clock_t t; 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 // PGCD
t = clock(); t = clock();
r = GCD(a, b); r = GCD(a, b);
...@@ -31,7 +41,6 @@ int main() ...@@ -31,7 +41,6 @@ int main()
printf("COMPUTED PGCD IN %lfs\n", timeT); printf("COMPUTED PGCD IN %lfs\n", timeT);
printf("PGCD(%ld, %ld) = %ld \n", a, b, r); printf("PGCD(%ld, %ld) = %ld \n", a, b, r);
// PPCM // PPCM
t = clock(); t = clock();
r = LCM(a, b); r = LCM(a, b);
...@@ -40,15 +49,13 @@ int main() ...@@ -40,15 +49,13 @@ int main()
printf("COMPUTED PPCM IN %lfs\n", timeT); printf("COMPUTED PPCM IN %lfs\n", timeT);
printf("PPCM(%ld, %ld) = %ld \n", a, b, r); printf("PPCM(%ld, %ld) = %ld \n", a, b, r);
// PRIME // FACTORIAL
t = clock(); t = clock();
for (int i = 0; i < 100; i++) r = fact(a);
{
printf("%d is %s\n", i, isPrime(i) ? "Prime": "Not Prime");
}
t = clock() - t; t = clock() - t;
timeT = ((double)t) / CLOCKS_PER_SEC; 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; 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 to comment