All Euler problems
Project Euler

Special Isosceles Triangles

Consider an isosceles triangle with base b, equal legs of length L, and height h (from the apex perpendicular to the base). The constraint is |b - h| = 1 (the base and height differ by exactly 1),...

Source sync Apr 19, 2026
Problem #0138
Level Level 05
Solved By 6,768
Languages C++, Python
Answer 1118049290473932
Length 332 words
sequencelinear_algebrageometry

Problem Statement

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

Consider the isosceles triangle with base length, $b = 16$, and legs, $L = 17$.

Problem illustration

By using the Pythagorean theorem it can be seen that the height of the triangle, $h = \sqrt{17^2 - 8^2} = 15$, which is one less than the base length.

With $b = 272$ and $L = 305$, we get $h = 273$, which is one more than the base length, and this is the second smallest isosceles triangle with the property that $h = b \pm 1$.

Find $\sum L$ for the twelve smallest isosceles triangles for which $h = b \pm 1$ and $b$, $L$ are positive integers.

Problem 138: Special Isosceles Triangles

Mathematical Foundation

Theorem 1. The Pythagorean relation L2=h2+(b/2)2L^2 = h^2 + (b/2)^2 combined with bh=1|b - h| = 1 and bb even (say b=2mb = 2m) leads to the Pell equation X25L2=1X^2 - 5L^2 = -1, where X=5m±2X = 5m \pm 2.

Proof. The height bisects the base, forming right triangles with legs hh and m=b/2m = b/2, and hypotenuse LL:

L2=h2+m2L^2 = h^2 + m^2

For b/2=mb/2 = m to be an integer, bb must be even. (If bb were odd, L2=h2+(b/2)2L^2 = h^2 + (b/2)^2 would require 4L2=4h2+b24L^2 = 4h^2 + b^2; checking modular conditions shows bb must be even for integer solutions.) Write b=2mb = 2m, so h=2m±1h = 2m \pm 1.

L2=(2m±1)2+m2=5m2±4m+1L^2 = (2m \pm 1)^2 + m^2 = 5m^2 \pm 4m + 1

Multiply by 5:

5L2=25m2±20m+5=(5m±2)2+15L^2 = 25m^2 \pm 20m + 5 = (5m \pm 2)^2 + 1

Setting X=5m±2X = 5m \pm 2:

X25L2=1X^2 - 5L^2 = -1

\square

Theorem 2. The fundamental solution of X25Y2=1X^2 - 5Y^2 = -1 is (X,Y)=(2,1)(X, Y) = (2, 1). All positive solutions are given by (Xn+Yn5)=(2+5)2n1(X_n + Y_n\sqrt{5}) = (2 + \sqrt{5})^{2n-1} for n=1,2,3,n = 1, 2, 3, \ldots.

Proof. The continued fraction expansion of 5=[2;4]\sqrt{5} = [2; \overline{4}] has period 1, which is odd, so x25y2=1x^2 - 5y^2 = -1 is solvable. The convergent p0/q0=2/1p_0/q_0 = 2/1 gives 45=14 - 5 = -1, confirming (2,1)(2, 1) as the fundamental solution. All solutions of X25Y2=1X^2 - 5Y^2 = -1 are obtained by taking odd powers of the fundamental element 2+52 + \sqrt{5}. Equivalently, using the fundamental solution (9,4)(9, 4) of X25Y2=1X^2 - 5Y^2 = 1 (obtained as (2+5)2(2 + \sqrt{5})^2), successive solutions of the 1-1 equation satisfy:

(Xn+1,Yn+1)=(9Xn+20Yn,  4Xn+9Yn)(X_{n+1}, Y_{n+1}) = (9X_n + 20Y_n, \; 4X_n + 9Y_n)

\square

Lemma 1. The YY-components (i.e., the LL-values) satisfy the linear recurrence Ln+1=18LnLn1L_{n+1} = 18L_n - L_{n-1} with characteristic polynomial λ218λ+1=0\lambda^2 - 18\lambda + 1 = 0.

Proof. The recurrence matrix for (X,Y)(X, Y) has eigenvalues 9±459 \pm 4\sqrt{5}. Since YnY_n is a linear combination of (9+45)n(9 + 4\sqrt{5})^n and (945)n(9 - 4\sqrt{5})^n, it satisfies the recurrence with characteristic equation λ218λ+1=0\lambda^2 - 18\lambda + 1 = 0 (trace = 18, determinant = 1 of the matrix (92049)\begin{pmatrix}9 & 20 \\ 4 & 9\end{pmatrix}). \square

Lemma 2. From each Pell solution (Xn,Ln)(X_n, L_n), the value m=(Xn2)/5m = (X_n \mp 2)/5 must be a positive integer. This requires Xn±2(mod5)X_n \equiv \pm 2 \pmod{5}.

Proof. Since X=5m±2X = 5m \pm 2, we need m=(X2)/5Z>0m = (X \mp 2)/5 \in \mathbb{Z}_{>0}. Checking: X1=22(mod5)X_1 = 2 \equiv 2 \pmod{5}. By the recurrence, Xn+1=9Xn+20Ln4Xn(mod5)X_{n+1} = 9X_n + 20L_n \equiv 4X_n \pmod{5}. Starting from X12X_1 \equiv 2: X23X_2 \equiv 3, X32X_3 \equiv 2, X43X_4 \equiv 3, … So Xn2X_n \equiv 2 or 3(mod5)3 \pmod{5}, i.e., Xn±2(mod5)X_n \equiv \pm 2 \pmod{5} always. Every Pell solution yields a valid triangle. \square

Editorial

The L values satisfy the recurrence L_{n+1} = 18*L_n - L_{n-1} with L_1 = 17, L_2 = 305. Sum the first 12 values. We advance to next solution. We then check m = (X - 2)/5 or (X + 2)/5 is positive integer. Finally, by Lemma 2, one always works.

Pseudocode

count = 12
Fundamental solution of X^2 - 5L^2 = -1
Advance to next solution
Check m = (X - 2)/5 or (X + 2)/5 is positive integer
By Lemma 2, one always works
else

Complexity Analysis

  • Time: O(k)O(k) arithmetic operations for k=12k = 12 terms. With big-integer arithmetic, bit complexity is O(k2)O(k^2) due to growing number sizes.
  • Space: O(k)O(k) bits to store the current values.

Answer

1118049290473932\boxed{1118049290473932}

Code

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

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

int main() {
    // L values satisfy L_{n+1} = 18*L_n - L_{n-1}
    // L_1 = 17, L_2 = 305
    // Sum the first 12 L values.

    long long L_prev = 17;
    long long L_curr = 305;
    long long total = L_prev + L_curr;

    for (int i = 3; i <= 12; i++) {
        long long L_next = 18 * L_curr - L_prev;
        total += L_next;
        L_prev = L_curr;
        L_curr = L_next;
    }

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