All Euler problems
Project Euler

Almost Equilateral Triangles

An almost equilateral triangle is a triangle with sides (a, a, a +/- 1) where a >= 2. Find the sum of the perimeters of all almost equilateral triangles with integral area whose perimeters do not e...

Source sync Apr 19, 2026
Problem #0094
Level Level 04
Solved By 14,550
Languages C++, Python
Answer 518408346
Length 462 words
geometrymodular_arithmeticsequence

Problem Statement

This archive keeps the full statement, math, and original media on the page.

It is easily proved that no equilateral triangle exists with integral length sides and integral area. However, the almost equilateral triangle \(5\)-\(5\)-\(6\) has an area of \(12\) square units.

We shall define an almost equilateral triangle to be a triangle for which two sides are equal and the third differs by no more than one unit.

Find the sum of the perimeters of all almost equilateral triangles with integral side lengths and area and whose perimeters do not exceed one billion (\(1\,000\,000\,000\)).

Problem 94: Almost Equilateral Triangles

Mathematical Foundation

Theorem 1 (Heron’s formula for isosceles triangles). An isosceles triangle with sides (a,a,b)(a, a, b) has area

A=b44a2b2.A = \frac{b}{4}\sqrt{4a^2 - b^2}.

Proof. The semi-perimeter is s=(2a+b)/2s = (2a + b)/2. By Heron’s formula:

A=s(sa)(sa)(sb)=2a+b2b2b22ab2=b4(2a+b)(2ab)=b44a2b2.A = \sqrt{s(s-a)(s-a)(s-b)} = \sqrt{\frac{2a+b}{2} \cdot \frac{b}{2} \cdot \frac{b}{2} \cdot \frac{2a-b}{2}} = \frac{b}{4}\sqrt{(2a+b)(2a-b)} = \frac{b}{4}\sqrt{4a^2 - b^2}. \quad \square

Lemma 1 (Case b=a+1b = a+1: integrality condition). For the triangle (a,a,a+1)(a, a, a+1), the area is a positive integer if and only if (3a+1)(a1)(3a+1)(a-1) is a perfect square (and a further divisibility condition holds).

Proof. Substituting b=a+1b = a+1 into Theorem 1:

A=a+144a2(a+1)2=a+143a22a1=a+14(3a+1)(a1).A = \frac{a+1}{4}\sqrt{4a^2 - (a+1)^2} = \frac{a+1}{4}\sqrt{3a^2 - 2a - 1} = \frac{a+1}{4}\sqrt{(3a+1)(a-1)}.

For AA to be a positive integer, we need (3a+1)(a1)=m2(3a+1)(a-1) = m^2 for some positive integer mm, giving A=(a+1)m/4A = (a+1)m/4, and we additionally require 4(a+1)m4 \mid (a+1)m. \square

Lemma 2 (Case b=a1b = a-1: integrality condition). For the triangle (a,a,a1)(a, a, a-1), the area is a positive integer if and only if (3a1)(a+1)(3a-1)(a+1) is a perfect square (with an analogous divisibility condition).

Proof. Substituting b=a1b = a - 1:

A=a144a2(a1)2=a14(3a1)(a+1).A = \frac{a-1}{4}\sqrt{4a^2 - (a-1)^2} = \frac{a-1}{4}\sqrt{(3a-1)(a+1)}. \quad \square

Theorem 2 (Reduction to Pell’s equation). Both integrality conditions reduce to the standard Pell equation X23Y2=1X^2 - 3Y^2 = 1.

Proof. Case 1 (b=a+1b = a+1): Set (3a+1)(a1)=m2(3a+1)(a-1) = m^2. Multiplying out: 3a22a1=m23a^2 - 2a - 1 = m^2. Multiply by 3: 9a26a3=3m29a^2 - 6a - 3 = 3m^2, whence (3a1)23m2=4(3a-1)^2 - 3m^2 = 4. Setting X=(3a1)/2X = (3a-1)/2 and Y=m/2Y = m/2 (whose integrality we verify for valid solutions), we obtain X23Y2=1X^2 - 3Y^2 = 1.

Case 2 (b=a1b = a-1): Set (3a1)(a+1)=m2(3a-1)(a+1) = m^2. Then 3a2+2a1=m23a^2 + 2a - 1 = m^2, so 9a2+6a3=3m29a^2 + 6a - 3 = 3m^2, giving (3a+1)23m2=4(3a+1)^2 - 3m^2 = 4. Setting X=(3a+1)/2X = (3a+1)/2, Y=m/2Y = m/2 yields X23Y2=1X^2 - 3Y^2 = 1. \square

Theorem 3 (Pell recurrence). The Pell equation X23Y2=1X^2 - 3Y^2 = 1 has fundamental solution (X1,Y1)=(2,1)(X_1, Y_1) = (2, 1). All positive solutions are generated by the linear recurrence

Xk+1=2Xk+3Yk,Yk+1=Xk+2Yk.X_{k+1} = 2X_k + 3Y_k, \qquad Y_{k+1} = X_k + 2Y_k.

Proof. One verifies directly that (2,1)(2,1) is the smallest positive solution. The recurrence corresponds to multiplication in the ring Z[3]\mathbb{Z}[\sqrt{3}]: (Xk+Yk3)(2+3)=(2Xk+3Yk)+(Xk+2Yk)3(X_k + Y_k\sqrt{3})(2 + \sqrt{3}) = (2X_k + 3Y_k) + (X_k + 2Y_k)\sqrt{3}. The norm N(X+Y3)=X23Y2N(X + Y\sqrt{3}) = X^2 - 3Y^2 is multiplicative, so if Xk23Yk2=1X_k^2 - 3Y_k^2 = 1 and 22312=12^2 - 3 \cdot 1^2 = 1, then

Xk+123Yk+12=(2Xk+3Yk)23(Xk+2Yk)2=Xk23Yk2=1.X_{k+1}^2 - 3Y_{k+1}^2 = (2X_k + 3Y_k)^2 - 3(X_k + 2Y_k)^2 = X_k^2 - 3Y_k^2 = 1.

All positive solutions arise this way by the theory of Pell equations (see, e.g., Niven, Zuckerman, and Montgomery, An Introduction to the Theory of Numbers, Theorem 7.26). \square

Theorem 4 (Recovery of triangle parameters). From each Pell solution (Xk,Yk)(X_k, Y_k):

  • If Xk1(mod3)X_k \equiv 1 \pmod{3}: Case 1 applies with a=(2Xk+1)/3a = (2X_k + 1)/3, perimeter =3a+1= 3a + 1.
  • If Xk2(mod3)X_k \equiv 2 \pmod{3}: Case 2 applies with a=(2Xk1)/3a = (2X_k - 1)/3, perimeter =3a1= 3a - 1.

Moreover, the residues alternate: Xkmod3{2,1,2,1,}X_k \bmod 3 \in \{2, 1, 2, 1, \ldots\} for k=1,2,3,k = 1, 2, 3, \ldots, so every Pell solution yields exactly one valid triangle.

Proof. For Case 1: X=(3a1)/2X = (3a-1)/2 gives a=(2X+1)/3a = (2X+1)/3, which is a positive integer if and only if X1(mod3)X \equiv 1 \pmod{3}. For Case 2: X=(3a+1)/2X = (3a+1)/2 gives a=(2X1)/3a = (2X-1)/3, requiring X2(mod3)X \equiv 2 \pmod{3}.

For the alternation, we proceed by induction. Base: X1=22(mod3)X_1 = 2 \equiv 2 \pmod{3}. Inductive step: Xk+1=2Xk+3Yk2Xk(mod3)X_{k+1} = 2X_k + 3Y_k \equiv 2X_k \pmod{3}. Since 2212 \cdot 2 \equiv 1 and 212(mod3)2 \cdot 1 \equiv 2 \pmod{3}, the residues alternate. \square

Editorial

The hard part is not searching over side lengths directly, but recognizing that the integral-area condition turns both a,a,a+1a,a,a+1 and a,a,a1a,a,a-1 into the same Pell-type recurrence. Once that reduction is done, the original geometric search space collapses to a simple sequence of Pell solutions.

Each Pell solution produces at most one almost-equilateral triangle, and the residue of XX modulo 33 tells us which of the two cases we are in. So the algorithm generates candidates by walking through Pell solutions in increasing size, converts each one back into a perimeter, filters by the appropriate congruence case, and stops as soon as the perimeter limit is exceeded.

Pseudocode

Initialize the Pell solution to the fundamental pair $(2,1)$.
Set the perimeter sum to 0.

Repeat:
    Inspect the current Pell solution

    If it corresponds to the $a,a,a+1$ case:
        recover $a$ and the perimeter from that formula
    Otherwise:
        recover $a$ and the perimeter from the $a,a,a-1$ formula

    If the perimeter exceeds the limit, stop
    If the triangle is nondegenerate, add its perimeter to the total

    Advance to the next Pell solution using the linear recurrence

Return the accumulated perimeter sum.

Complexity Analysis

Time: Solutions to the Pell equation grow exponentially as Xk(2+3)k/2X_k \sim (2 + \sqrt{3})^k / 2. The number of solutions with perimeter L\le L is Θ(logL)\Theta(\log L). Each iteration performs O(1)O(1) arithmetic. Total: O(logL)O(\log L).

Space: O(1)O(1) (two variables for the current Pell solution).

Answer

518408346\boxed{518408346}

Code

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

C++ project_euler/problem_094/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // Almost equilateral triangles: sides (a, a, a+/-1) with integral area.
    // Both cases reduce to Pell equation X^2 - 3Y^2 = 1.
    // Fundamental solution: (X,Y) = (2,1).
    // Recurrence: X' = 2X + 3Y, Y' = X + 2Y.
    //
    // X mod 3 == 1 => Case 1: a = (2X+1)/3, perimeter = 3a+1
    // X mod 3 == 2 => Case 2: a = (2X-1)/3, perimeter = 3a-1

    const long long LIMIT = 1000000000LL;
    long long total = 0;
    long long x = 2, y = 1;

    while (true) {
        long long a, perim;
        if (x % 3 == 1) {
            a = (2 * x + 1) / 3;
            perim = 3 * a + 1;
        } else {
            a = (2 * x - 1) / 3;
            perim = 3 * a - 1;
        }
        if (perim > LIMIT) break;
        if (a >= 2) total += perim;

        long long nx = 2 * x + 3 * y;
        long long ny = x + 2 * y;
        x = nx;
        y = ny;
    }

    cout << total << endl;
    return 0;
}