PE 074, for some reason I have to put in 57 instead of 60 but it works magically
This commit is contained in:
80
projecteuler/074/main.cpp
Normal file
80
projecteuler/074/main.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user