PE 075, pretty neat

This commit is contained in:
2022-04-22 15:04:18 +02:00
parent bd57e0590e
commit c9d461ba75

56
projecteuler/075/main.cpp Normal file
View File

@@ -0,0 +1,56 @@
#include <bits/stdc++.h>
using namespace std;
int gcd(const int a, const int b){
if(b == 0){
return a;
}
return gcd(b, a % b);
}
int main(){
cout << "Hello this is Patrick" << endl;
auto start = chrono::high_resolution_clock::now();
// Generating pythagorean triplets can be done with Euclid's formula
int maxLength = 1500000;
vector<int> occurrences(maxLength + 1, 0);
for(int i = 1; i < 1500; ++i){
for(int j = 1; j < i; ++j){
if(gcd(i, j) != 1 || ((i % 2) != 0 && (j % 2) != 0)){
continue;
}
for(int k = 1; k < maxLength / i; ++k){
long a = k * (i*i - j*j);
long b = 2 * k * i * j;
long c = k * (i*i + j*j);
long l = a + b + c;
if(l < occurrences.size()){
occurrences[l]++;
// cout << a << " " << b << " " << c << " " << l << endl;
}
}
}
}
int result = 0;
for(auto o : occurrences){
if(o == 1){
result++;
}
}
cout << result << endl;
auto duration = chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start);
cout << (float)duration.count()/1000 << endl;
return 0;
}