Last active
September 6, 2019 10:23
-
-
Save andraantariksa/43d5a5fda7c910bf624a896f3a3371c7 to your computer and use it in GitHub Desktop.
Factorial Recursive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstring> | |
#include <cstdio> | |
#include <cstdlib> | |
int long solve_factorial(long int *factorial_memoization, int length, int long n = 1, int index = 0) | |
{ | |
*(factorial_memoization +index) = n; | |
// printf("Check of %d is %ld\n", index, factorial_memoization[index]); | |
if (index == length) | |
{ | |
return index; | |
} | |
if (index == 0) | |
{ | |
return 1 * solve_factorial(factorial_memoization, length, n * (index + 1), index + 1); | |
} | |
return index * solve_factorial(factorial_memoization, length, n * (index + 1), index + 1); | |
} | |
int main() | |
{ | |
long int *factorial_memoization; | |
long int element_total; | |
scanf("%ld", &element_total); | |
factorial_memoization = (long int*) malloc(sizeof(long int) * (element_total + 1)); | |
long int result = solve_factorial(factorial_memoization, element_total); | |
for (int i = 0; i <= element_total; i++) | |
{ | |
printf("Factorial of %d is %ld\n", i, *(factorial_memoization + i)); | |
} | |
printf("Factorial(%ld) result %ld", element_total, result); | |
free(factorial_memoization); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment