Integer Sided Triangles for which Area/Perimeter Ratio is Integral
Find the sum of perimeters of all integer-sided triangles for which A/P = k is a positive integer with 1 <= k <= 1000, where A is the area and P is the perimeter.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Consider the triangle with sides \(6\), \(8\), and \(10\). It can be seen that the perimeter and the area are both equal to \(24\). So the area/perimeter ratio is equal to \(1\).
Consider also the triangle with sides \(13\), \(14\) and \(15\). The perimeter equals \(42\) while the area is equal to \(84\). So for this triangle the area/perimeter ratio is equal to \(2\).
Find the sum of the perimeters of all integer sided triangles for which the area/perimeter ratios are equal to positive integers not exceeding \(1000\).
Problem 283: Integer Sided Triangles for which Area/Perimeter Ratio is Integral
Mathematical Foundation
Theorem (Reduction to Diophantine Equation). Let be the integer side lengths of a triangle with semi-perimeter . If for a positive integer , then defining , , with , we have
the sides are , , , the perimeter is , and are either all integers (even perimeter) or all half-integers (odd perimeter).
Proof. By Heron’s formula, . The condition gives , so , hence . Substituting , , yields .
Since are positive integers, is a positive integer. If is even, then is an integer and are positive integers. If is odd, then is a half-integer and are positive half-integers.
The triangle inequality is automatically satisfied: , etc. The ordering corresponds to (up to reindexing).
Lemma (Enumeration Bounds). In the equation with :
- ,
- requires ,
- .
Proof. From , we get , and also . More directly, since , we have and , giving , so , hence .
For , solving gives , requiring (otherwise or undefined), and . The condition then constrains .
Lemma (Odd Perimeter Case). When are half-integers, setting , , (odd positive integers with ) yields
Proof. Direct substitution into with , etc.
Editorial
For triangle with integer sides a,b,c, let s=(a+b+c)/2, A=sqrt(s(s-a)(s-b)(s-c)). Need A/(2s) = k (positive integer, 1 <= k <= 1000). Substituting x=s-a, y=s-b, z=s-c (with 0<x<=y<=z): xyz = 4k^2(x+y+z), P = 2(x+y+z) Case 1 (even P): x,y,z positive integers. z = 4k^2(x+y) / (xy - 4k^2) Case 2 (odd P): x=X/2, y=Y/2, z=Z/2, X,Y,Z odd positive integers. XYZ = 16k^2(X+Y+Z), Z = 16k^2(X+Y)/(XY-16k^2) Sum all perimeters of valid triangles. We case 1: even perimeter (x, y, z positive integers). Finally, case 2: odd perimeter (X, Y, Z odd positive integers).
Pseudocode
Case 1: even perimeter (x, y, z positive integers)
Case 2: odd perimeter (X, Y, Z odd positive integers)
Complexity Analysis
- Time: where . For each , the inner loops run iterations on average (bounded by the divisor structure).
- Space: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 lll;
int main() {
ll total = 0;
for (ll k = 1; k <= 1000; k++) {
ll k2 = k * k;
// Case 1: even perimeter, x,y,z positive integers
// xyz = 4k^2(x+y+z), x <= y <= z
// z = 4k^2(x+y) / (xy - 4k^2)
ll c = 4 * k2;
ll x_max = (ll)sqrt(3.0 * c) + 2;
for (ll x = 1; x <= x_max; x++) {
ll y_start = max(x, c / x + 1);
// y upper bound from z >= y
// y <= (c + sqrt(c^2 + c*x^2)) / x
double disc = (double)c * ((double)c + (double)x * x);
ll y_end = (ll)((c + sqrt(disc)) / x) + 2;
for (ll y = y_start; y <= y_end; y++) {
ll d = x * y - c;
if (d <= 0) continue;
ll n = c * (x + y);
if (n % d != 0) continue;
ll z = n / d;
if (z < y) break;
total += 2 * (x + y + z);
}
}
// Case 2: odd perimeter, X,Y,Z odd positive integers
// XYZ = 16k^2(X+Y+Z), X <= Y <= Z
// Z = 16k^2(X+Y) / (XY - 16k^2)
ll c2 = 16 * k2;
ll X_max = (ll)sqrt(3.0 * c2) + 2;
for (ll X = 1; X <= X_max; X += 2) {
ll Y_start = max(X, c2 / X + 1);
if (Y_start % 2 == 0) Y_start++;
double disc2 = (double)c2 * ((double)c2 + (double)X * X);
ll Y_end = (ll)((c2 + sqrt(disc2)) / X) + 2;
for (ll Y = Y_start; Y <= Y_end; Y += 2) {
ll d = X * Y - c2;
if (d <= 0) continue;
ll n = c2 * (X + Y);
if (n % d != 0) continue;
ll Z = n / d;
if (Z < Y) break;
if (Z % 2 == 0) continue;
total += X + Y + Z; // P = X+Y+Z
}
}
}
cout << total << endl;
return 0;
}
"""
Project Euler Problem 283: Integer Sided Triangles for which Area/Perimeter is Integral
For triangle with integer sides a,b,c, let s=(a+b+c)/2, A=sqrt(s(s-a)(s-b)(s-c)).
Need A/(2s) = k (positive integer, 1 <= k <= 1000).
Substituting x=s-a, y=s-b, z=s-c (with 0<x<=y<=z):
xyz = 4k^2(x+y+z), P = 2(x+y+z)
Case 1 (even P): x,y,z positive integers.
z = 4k^2(x+y) / (xy - 4k^2)
Case 2 (odd P): x=X/2, y=Y/2, z=Z/2, X,Y,Z odd positive integers.
XYZ = 16k^2(X+Y+Z), Z = 16k^2(X+Y)/(XY-16k^2)
Sum all perimeters of valid triangles.
"""
from math import isqrt, sqrt
def solve():
total = 0
for k in range(1, 1001):
k2 = k * k
# Case 1: even perimeter
c = 4 * k2
x_max = int(sqrt(3.0 * c)) + 2
for x in range(1, x_max + 1):
y_start = max(x, c // x + 1)
disc = c * (c + x * x)
y_end = int((c + sqrt(disc)) / x) + 2
for y in range(y_start, y_end + 1):
d = x * y - c
if d <= 0:
continue
n = c * (x + y)
if n % d != 0:
continue
z = n // d
if z < y:
break
total += 2 * (x + y + z)
# Case 2: odd perimeter
c2 = 16 * k2
X_max = int(sqrt(3.0 * c2)) + 2
for X in range(1, X_max + 1, 2):
Y_start = max(X, c2 // X + 1)
if Y_start % 2 == 0:
Y_start += 1
disc2 = c2 * (c2 + X * X)
Y_end = int((c2 + sqrt(disc2)) / X) + 2
for Y in range(Y_start, Y_end + 1, 2):
d = X * Y - c2
if d <= 0:
continue
n = c2 * (X + Y)
if n % d != 0:
continue
Z = n // d
if Z < Y:
break
if Z % 2 == 0:
continue
total += X + Y + Z
print(total)
solve()