mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Added recursive factorial calculator (#1326)
This commit is contained in:
parent
e5dad3fa8d
commit
73dd771a0b
40
math/factorial_recursion.c
Normal file
40
math/factorial_recursion.c
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
// finds factorial of a given inegral input. Assumes 0!=1.
|
||||||
|
int factorial(int n)
|
||||||
|
{
|
||||||
|
if (n == 0)
|
||||||
|
{
|
||||||
|
return 1; // base case
|
||||||
|
}
|
||||||
|
return n * factorial(n - 1); // based on the fact that n*(n-1)! = n!
|
||||||
|
}
|
||||||
|
|
||||||
|
bool check_arrays(int *a, int *b, int pos)
|
||||||
|
{
|
||||||
|
if (factorial(a[pos]) == b[pos])
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void tests()
|
||||||
|
{
|
||||||
|
/* tests have a few integral factorials*/
|
||||||
|
int tests[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
|
||||||
|
int expected[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800};
|
||||||
|
|
||||||
|
for (int i = 0; i < 11; i++)
|
||||||
|
{
|
||||||
|
assert(check_arrays(tests, expected, i) == true);
|
||||||
|
}
|
||||||
|
printf("All test cases passed !\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
tests();
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user