fork download
#include <iostream>
#include <vector>
using namespace std;

static vector<long long> cache;

long long factorial(long long n){

     if (n>cache.size()-1){
        cache.push_back(n*factorial(n-1));
     }
     
     return cache[n];
}

int main(){
    cache.push_back(1);
    cache.push_back(1);
    
    cout << factorial(3) << endl;
    cout << factorial(4) << endl;
    cin.get();
    return 0;
}
Success #stdin #stdout 0.01s 2860KB
stdin
Standard input is empty
stdout
6
24