81 lines
1.6 KiB
C++
81 lines
1.6 KiB
C++
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
const vector<int> facs{1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
|
|
set<int> 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<int> 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::milliseconds>(chrono::high_resolution_clock::now() - start);
|
|
cout << (float)duration.count()/1000 << endl;
|
|
return 0;
|
|
}
|