All Euler problems
Project Euler

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.

Source sync Apr 19, 2026
Problem #0283
Level Level 18
Solved By 750
Languages C++, Python
Answer 28038042525570324
Length 307 words
geometrymodular_arithmeticanalytic_math

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 a,b,ca, b, c be the integer side lengths of a triangle with semi-perimeter s=(a+b+c)/2s = (a+b+c)/2. If A/P=kA/P = k for a positive integer kk, then defining x=sax = s-a, y=sby = s-b, z=scz = s-c with 0<xyz0 < x \le y \le z, we have

xyz=4k2(x+y+z),xyz = 4k^2(x + y + z),

the sides are a=y+za = y+z, b=x+zb = x+z, c=x+yc = x+y, the perimeter is P=2(x+y+z)P = 2(x+y+z), and x,y,zx, y, z are either all integers (even perimeter) or all half-integers (odd perimeter).

Proof. By Heron’s formula, A=s(sa)(sb)(sc)A = \sqrt{s(s-a)(s-b)(s-c)}. The condition A=kP=2ksA = kP = 2ks gives A2=4k2s2A^2 = 4k^2 s^2, so s(sa)(sb)(sc)=4k2s2s(s-a)(s-b)(s-c) = 4k^2 s^2, hence (sa)(sb)(sc)=4k2s(s-a)(s-b)(s-c) = 4k^2 s. Substituting x=sax = s-a, y=sby = s-b, z=scz = s-c yields xyz=4k2(x+y+z)xyz = 4k^2(x+y+z).

Since a,b,ca, b, c are positive integers, 2s=a+b+c2s = a+b+c is a positive integer. If 2s2s is even, then ss is an integer and x,y,zx, y, z are positive integers. If 2s2s is odd, then ss is a half-integer and x,y,zx, y, z are positive half-integers.

The triangle inequality is automatically satisfied: a+bc=2z>0a + b - c = 2z > 0, etc. The ordering xyzx \le y \le z corresponds to abca \ge b \ge c (up to reindexing). \quad\square

Lemma (Enumeration Bounds). In the equation xyz=4k2(x+y+z)xyz = 4k^2(x+y+z) with 0<xyz0 < x \le y \le z:

  1. x2k3x \le 2k\sqrt{3},
  2. y4k2(x+y)xy4k2y \le \frac{4k^2(x+y)}{xy - 4k^2} requires xy>4k2xy > 4k^2,
  3. z=4k2(x+y)xy4k2z = \frac{4k^2(x+y)}{xy - 4k^2}.

Proof. From xyzx \le y \le z, we get x3xyz=4k2(x+y+z)4k23zx^3 \le xyz = 4k^2(x+y+z) \le 4k^2 \cdot 3z, and also zxyz/x2=4k2(x+y+z)/x2z \le xyz/x^2 = 4k^2(x+y+z)/x^2. More directly, since zyxz \ge y \ge x, we have xyzx3xyz \ge x^3 and x+y+z3zx+y+z \le 3z, giving x2y12k2x^2 y \le 12k^2, so x312k2x^3 \le 12k^2, hence x(12k2)1/32k3x \le (12k^2)^{1/3} \le 2k\sqrt{3}.

For zz, solving xyz=4k2(x+y+z)xyz = 4k^2(x+y+z) gives z(xy4k2)=4k2(x+y)z(xy - 4k^2) = 4k^2(x+y), requiring xy>4k2xy > 4k^2 (otherwise z0z \le 0 or undefined), and z=4k2(x+y)xy4k2z = \frac{4k^2(x+y)}{xy - 4k^2}. The condition zyz \ge y then constrains yy. \quad\square

Lemma (Odd Perimeter Case). When x,y,zx, y, z are half-integers, setting X=2xX = 2x, Y=2yY = 2y, Z=2zZ = 2z (odd positive integers with XYZX \le Y \le Z) yields

XYZ=16k2(X+Y+Z),Z=16k2(X+Y)XY16k2.XYZ = 16k^2(X + Y + Z), \quad Z = \frac{16k^2(X+Y)}{XY - 16k^2}.

Proof. Direct substitution into xyz=4k2(x+y+z)xyz = 4k^2(x+y+z) with x=X/2x = X/2, etc. \quad\square

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: O(K2logK)O(K^2 \log K) where K=1000K = 1000. For each kk, the inner loops run O(klogk)O(k \log k) iterations on average (bounded by the divisor structure).
  • Space: O(1)O(1).

Answer

28038042525570324\boxed{28038042525570324}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_283/solution.cpp
#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;
}