Finished 62 in a jiffy, wasn't hard when using a sorted list to keep track of the found cubes

This commit is contained in:
2021-11-02 13:50:35 +01:00
parent f649acf5a3
commit 7430bccec9

48
projecteuler/062/main.cpp Normal file
View File

@@ -0,0 +1,48 @@
/*
Cubic Permutations
The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.
Find the smallest cube for which exactly five permutations of its digits are cube.
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
cout << "Hello this is Patrick" << endl;
auto start = chrono::high_resolution_clock::now();
map<string, int> occs;
map<string, vector<ll>> cubes;
int n = 1;
ll cube, result;
string s;
while(true){
cube = pow(n, 3);
s = to_string(cube);
sort(s.begin(), s.end());
occs[s]++;
cubes[s].push_back(cube);
if(occs[s] == 5){
result = cubes[s].front();
break;
}
n++;
}
cout << result << endl;
auto duration = chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start);
cout << (float)duration.count()/1000 << endl;
return 0;
}