-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoFactorial.cpp
More file actions
37 lines (30 loc) · 760 Bytes
/
Copy pathMemoFactorial.cpp
File metadata and controls
37 lines (30 loc) · 760 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//This program calculate the 21! using memorization and recursion.
//21! is the largest factorial to be stored in normal C++
#include <iostream>
#include <algorithm>
using namespace std;
//The memory array
unsigned long long int memo[22];
//Inutilizing the array
void initializeMemo(){
fill(memo, memo + 22, -1);
}
//Calculating the factorial
unsigned long long int factorial(int n){
if (memo[n] != -1) {
return memo[n];
}
if (n < 2) {
return 1;
} else {
memo[n] = n * factorial(n - 1);
return memo[n];
}
}
//The driver function
int main() {
initializeMemo();
//Printing the largest possible number
cout << "The factorial of 21 is "<< factorial(21);
return 0;
}