Commit 34d6c0f9 authored by Jonas's avatar Jonas
Browse files

Added factorial

parent 350c8106
No preview for this file type
......@@ -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
......@@ -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;
}
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment