diff --git a/projecteuler/074/main.cpp b/projecteuler/074/main.cpp new file mode 100644 index 0000000..54a1341 --- /dev/null +++ b/projecteuler/074/main.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +const vector facs{1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880}; +set loopers; + +// Only for factorials for 0 <= i <= 9 +int customFac(const int i){ + if(i < 0 || i > 9){ + return 0; + } + return facs[i]; +} + +int facSum(int i){ + if(i == 0){ + return 1; + } + + int result = 0; + while(i > 0){ + int a = i % 10; + i /= 10; + + result += customFac(a); + } + + return result; +} + +int nonRepeatingChain(int i){ + if(loopers.count(i)){ + return 1; + } + + int chain; + set visited{i}; + + chain = facSum(i); + + for(;;){ + // cout << chain << " "; + + if(loopers.count(chain) || visited.count(chain)){ + // cout << endl; + + loopers.insert(chain); + return visited.size(); + } + + visited.insert(chain); + chain = facSum(chain); + } +} + +int main(){ + + cout << "Hello this is Patrick" << endl; + auto start = chrono::high_resolution_clock::now(); + + // cout << nonRepeatingChain(169) << endl; + + int result = 0; + for(int i = 1; i < 1000000; ++i){ + if(nonRepeatingChain(i) == 57){ + result++; + } + } + + cout << result << endl; + cout << "Loopers: "; + for(auto l : loopers){ + cout << l << " "; + } cout << endl; + + auto duration = chrono::duration_cast(chrono::high_resolution_clock::now() - start); + cout << (float)duration.count()/1000 << endl; + return 0; +}