#include #include long ggd(long x, long y) { while (x != y) if (x > y) x -= y; else y -= x; return x; } int twopyth(long area, long a, long b, long c) { if ((2*area) % a == 0) { long h = (2*area) / a; long bh = floor(sqrt(b*b - h*h)+0.001); long ch = floor(sqrt(c*c - h*h)+0.001); if ( bh*bh + h*h == b*b && ch*ch + h*h == c*c) { printf(" two pyth: %ld,%ld,%ld", bh, b, h); printf(" and %ld,%ld,%ld", ch, c, h); return 1; } } return 0; } #define SQR_DIST(X,Y) ((X)*(X) + (Y)*(Y)) int insquare(long area, long a, long b, long c) { long a_2 = a*a; long a1,a2 = a-1; for (a1 = 1; a1 < a; a1++) { long a1_2 = a1*a1; while (a1_2 + a2*a2 > a_2) a2--; if (a1_2 + a2*a2 == a_2) { long b_2 = b*b; long b1,b2 = b-1; for (b1 = 1; b1 < b; b1++) { long b1_2 = b1*b1; while (b1_2 + b2*b2 > b_2) b2--; if (b1_2 + b2*b2 == b_2 && b2 > a1) { long c1 = b2 - a1; long c2 = a2 + b1; if (SQR_DIST(c1,c2) == c*c) { if (area + (a1*a2 + b1*b2 + c1*c2)/2 == b2*c2) { printf(" in square %d,%d", b2,c2); return 1; } } } } } } return 0; } int ongrid(long a, long b, long c) { long c_2 = c*c; long a_2 = a*a; long a1,a2 = a-1; for (a1 = 1; a1 < a; a1++) { long a1_2 = a1*a1; while (a1_2 + a2*a2 > a_2) a2--; if (a1_2 + a2*a2 == a_2) { long b_2 = b*b; long b1,b2 = b-1; for (b1 = 1; b1 < b; b1++) { long b1_2 = b1*b1; while (b1_2 + b2*b2 > b_2) b2--; if (b1_2 + b2*b2 == b_2) { if (SQR_DIST(a1-b1,a2-b2) == c_2) { printf(" on grid 0,0 %d,%d %d,%d", a1,a2, b1,b2); return 1; } if (SQR_DIST(a1+b1,a2-b2) == c_2) { printf(" on grid 0,0 -%d,%d %d,%d", a1,a2, b1,b2); return 1; } if (SQR_DIST(a1-b1,a2+b2) == c_2) { printf(" on grid 0,0 %d,-%d %d,%d", a1,a2, b1,b2); return 1; } if (SQR_DIST(a1+b1,a2+b2) == c_2) { printf(" on grid 0,0 -%d,-%d %d,%d", a1,a2, b1,b2); return 1; } } } } } return 0; } main() { long s, a, b, c; for (s = 4; s <= 1000; s += 2) { long s2 = s/2; for (a = 1; a + a + a <= s; a++) for (b = a; a + b + b <= s; b++) { c = s - a - b; if ( c >= b && (a + b > c) && (a + c > b) && (b + c > a) && ggd(a, ggd(b, c)) == 1) { long area2 = s2 * (s2-a) * (s2-b) * (s2-c); long area = floor(sqrt(area2)+0.001); if (area*area == area2) { printf("%6ld: %ld %ld %ld = %ld", area, a, b, c, s); if (a*a + b*b == c*c) printf(" pyth"); else if ( !twopyth(area, a, b, c) && !twopyth(area, b, a, c) && !twopyth(area, c, a, b) && !insquare(area, a,b,c) && !ongrid(a,b,c)) { printf(" not on grid"); } printf("\n"); } } } } }