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...
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
We shall define an
Find the sum of the perimeters of all
Problem 94: Almost Equilateral Triangles
Mathematical Foundation
Theorem 1 (Heron’s formula for isosceles triangles). An isosceles triangle with sides has area
Proof. The semi-perimeter is . By Heron’s formula:
Lemma 1 (Case : integrality condition). For the triangle , the area is a positive integer if and only if is a perfect square (and a further divisibility condition holds).
Proof. Substituting into Theorem 1:
For to be a positive integer, we need for some positive integer , giving , and we additionally require .
Lemma 2 (Case : integrality condition). For the triangle , the area is a positive integer if and only if is a perfect square (with an analogous divisibility condition).
Proof. Substituting :
Theorem 2 (Reduction to Pell’s equation). Both integrality conditions reduce to the standard Pell equation .
Proof. Case 1 (): Set . Multiplying out: . Multiply by 3: , whence . Setting and (whose integrality we verify for valid solutions), we obtain .
Case 2 (): Set . Then , so , giving . Setting , yields .
Theorem 3 (Pell recurrence). The Pell equation has fundamental solution . All positive solutions are generated by the linear recurrence
Proof. One verifies directly that is the smallest positive solution. The recurrence corresponds to multiplication in the ring : . The norm is multiplicative, so if and , then
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).
Theorem 4 (Recovery of triangle parameters). From each Pell solution :
- If : Case 1 applies with , perimeter .
- If : Case 2 applies with , perimeter .
Moreover, the residues alternate: for , so every Pell solution yields exactly one valid triangle.
Proof. For Case 1: gives , which is a positive integer if and only if . For Case 2: gives , requiring .
For the alternation, we proceed by induction. Base: . Inductive step: . Since and , the residues alternate.
Editorial
The hard part is not searching over side lengths directly, but recognizing that the integral-area condition turns both and 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 modulo 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 . The number of solutions with perimeter is . Each iteration performs arithmetic. Total: .
Space: (two variables for the current Pell solution).
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Project Euler Problem 94: Almost Equilateral Triangles
Find the sum of perimeters of all almost equilateral triangles (sides a,a,a+/-1)
with integral area and perimeter not exceeding 10^9.
Both cases reduce to the Pell equation X^2 - 3Y^2 = 1 with fundamental
solution (2,1) and recurrence X' = 2X + 3Y, Y' = X + 2Y.
Answer: 518408346
"""
def solve(limit=1_000_000_000):
total = 0
x, y = 2, 1 # fundamental Pell solution
while True:
if x % 3 == 1:
# Case b = a+1: a = (2x+1)/3, perimeter = 3a+1
a = (2 * x + 1) // 3
perim = 3 * a + 1
if perim > limit:
break
if a >= 2:
total += perim
else:
# Case b = a-1: a = (2x-1)/3, perimeter = 3a-1
a = (2 * x - 1) // 3
perim = 3 * a - 1
if perim > limit:
break
if a >= 2:
total += perim
x, y = 2 * x + 3 * y, x + 2 * y
return total
if __name__ == "__main__":
answer = solve()
assert answer == 518408346
print(answer)