project-euler/a091.cpp

44 lines
1012 B
C++

#include <iostream>
#include <cmath>
#include <chrono>
#define sq 3
using namespace std;
typedef unsigned char byte;
template<class T>
inline T gcd(T a, T b) {
while(b) {
auto t = a % b;
a = b;
b = t;
}
return a;
}
int main() {
auto start = chrono::system_clock::now();
//triangles for which either (x1,y1) == (0,0)
//or x1==0,y1!=0 or x1!=0,y1==0
long count = 3*sq*sq;
for (int vx = 1; vx <= sq; vx++)
for (int vy = 1; vy <= sq; vy++) {
int dx = -vy/gcd(vx,vy);
int dy = vx/gcd(vx,vy);
for (int d = 1; vx+dx*d >= 0 && vy+dy*d <= sq; d++)
count++;
for (int d = -1; vx+dx*d <= sq && vy+dy*d >= 0; d--)
count++;
}
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
cout << "solving took: " << elapsed_seconds.count() << endl;
cout << "triangles: " << count;
return 0;
}