Open Source · 7 Languages · 900+ Problems

Project Euler Solutions

Complete solutions in C++, Python & Java — with step-by-step mathematical explanations

All Problems

Problem 138: Special Isosceles Triangles

View on Project Euler

Project Euler Problem 138 Solution

EulerSolve provides an optimized solution for Project Euler Problem 138, Special Isosceles Triangles, with C++, Python, Java, and a step-by-step mathematical explanation.

Problem Summary We seek isosceles triangles with equal sides \(L\), base \(b\), and altitude \(h\) such that \(h=b\pm1\). The triangles are ordered by increasing \(L\), and the required result is the sum of the first twelve leg lengths. The altitude of an isosceles triangle bisects the base, so the geometry immediately becomes a right-triangle problem. The valid triangles do not need to be found by brute force; they form a Pell-type sequence, and the implementations use the resulting recurrence directly. Mathematical Approach Let \(m=b/2\). Since \(4L^2=4h^2+b^2\), the base must be even, so \(m\) is an integer. Write the sign in the condition \(h=b\pm1\) as \(\sigma\in\{-1,+1\}\), which lets us express the height as \(h=2m+\sigma\). From the Triangle to a Pell Equation Splitting the isosceles triangle along its altitude gives a right triangle with legs \(m\) and \(h\), and hypotenuse \(L\). Therefore $L^2=m^2+(2m+\sigma)^2=5m^2+4\sigma m+1.$ Multiply by \(5\) and complete the square: $5L^2=25m^2+20\sigma m+5=(5m+2\sigma)^2+1.$ If we define $X=5m+2\sigma,$ then every special isosceles triangle produces a solution of $X^2-5L^2=-1.$ So the geometric problem is equivalent to finding the positive nondegenerate solutions of the negative Pell equation \(X^2-5Y^2=-1\), with \(Y=L\). Recovering the Triangle from a Pell Solution The converse is just as important....

Detailed mathematical approach

Problem Summary

We seek isosceles triangles with equal sides \(L\), base \(b\), and altitude \(h\) such that \(h=b\pm1\). The triangles are ordered by increasing \(L\), and the required result is the sum of the first twelve leg lengths.

The altitude of an isosceles triangle bisects the base, so the geometry immediately becomes a right-triangle problem. The valid triangles do not need to be found by brute force; they form a Pell-type sequence, and the implementations use the resulting recurrence directly.

Mathematical Approach

Let \(m=b/2\). Since \(4L^2=4h^2+b^2\), the base must be even, so \(m\) is an integer. Write the sign in the condition \(h=b\pm1\) as \(\sigma\in\{-1,+1\}\), which lets us express the height as \(h=2m+\sigma\).

From the Triangle to a Pell Equation

Splitting the isosceles triangle along its altitude gives a right triangle with legs \(m\) and \(h\), and hypotenuse \(L\). Therefore

$L^2=m^2+(2m+\sigma)^2=5m^2+4\sigma m+1.$

Multiply by \(5\) and complete the square:

$5L^2=25m^2+20\sigma m+5=(5m+2\sigma)^2+1.$

If we define

$X=5m+2\sigma,$

then every special isosceles triangle produces a solution of

$X^2-5L^2=-1.$

So the geometric problem is equivalent to finding the positive nondegenerate solutions of the negative Pell equation \(X^2-5Y^2=-1\), with \(Y=L\).

Recovering the Triangle from a Pell Solution

The converse is just as important. Any solution of \(X^2-5L^2=-1\) satisfies \(X^2\equiv4\pmod{5}\), so \(X\equiv\pm2\pmod{5}\). That congruence determines which sign occurs in \(h=b\pm1\): if \(X\equiv2\pmod{5}\), then \(\sigma=+1\); if \(X\equiv-2\pmod{5}\), then \(\sigma=-1\).

Once \(\sigma\) is known, the triangle parameters are recovered by

$m=\frac{X-2\sigma}{5},\qquad b=2m,\qquad h=2m+\sigma=b+\sigma.$

The smallest Pell solution \((X,L)=(2,1)\) gives \(m=0\), so it is degenerate and must be discarded. Every later positive solution yields a genuine triangle.

Generating All Solutions

The equation \(X^2-5L^2=-1\) has fundamental solution \(2^2-5\cdot1^2=-1\). Consecutive nondegenerate triangle solutions are obtained by multiplying by

$9+4\sqrt{5}=(2+\sqrt{5})^2.$

Thus, if \(X_n+L_n\sqrt{5}\) represents one triangle, then the next triangle satisfies

$X_{n+1}+L_{n+1}\sqrt{5}=(9+4\sqrt{5})(X_n+L_n\sqrt{5}).$

Expanding gives the two-variable recurrence

$X_{n+1}=9X_n+20L_n,\qquad L_{n+1}=4X_n+9L_n,$

with the first nondegenerate pair \((X_1,L_1)=(38,17)\).

Eliminating \(X_n\) yields the scalar recurrence used by the implementations:

$L_{n+2}=18L_{n+1}-L_n,\qquad L_1=17,\qquad L_2=305.$

This generates the required leg lengths \(17,305,5473,98209,\dots\). Equivalently, \(L_n=F_{6n+3}/2\), but the second-order recurrence is the most direct computational form.

Worked Example and the Alternating Sign

For the first triangle, \((X_1,L_1)=(38,17)\). Since \(38\equiv-2\pmod{5}\), we take \(\sigma=-1\), and then

$m=\frac{38+2}{5}=8,\qquad b=16,\qquad h=15.$

Indeed, \(15^2+8^2=17^2\), so this is a valid triangle with \(h=b-1\).

Multiply once by \(9+4\sqrt{5}\) to get the next solution:

$38+17\sqrt{5}\to682+305\sqrt{5}.$

Now \(682\equiv2\pmod{5}\), so \(\sigma=+1\), and

$m=\frac{682-2}{5}=136,\qquad b=272,\qquad h=273.$

This second triangle satisfies \(h=b+1\). Because \(X_{n+1}\equiv-X_n\pmod{5}\), the residues \(2\) and \(-2\) alternate, so the family alternates between the cases \(h=b-1\) and \(h=b+1\).

How the Code Works

The C++, Python, and Java implementations skip the Pell algebra at runtime and start directly from the first two leg lengths \(17\) and \(305\). A running sum is initialized from the first term because the only requested output is the sum of the first twelve \(L\)-values.

Each loop iteration adds the current term, computes the next one with \(L_{n+2}=18L_{n+1}-L_n\), and shifts the two previous terms forward. The C++ implementation also contains a small built-in checkpoint on the first two terms and can vary the number of generated triangles, while the Python and Java implementations are fixed to twelve. In every language, the program is simply a compact encoding of the Pell-generated sequence.

Complexity Analysis

For the required twelve triangles, the running time is constant. More generally, producing the first \(N\) leg lengths by the recurrence takes \(O(N)\) time and \(O(1)\) extra space.

The values grow roughly like \((9+4\sqrt{5})^n\), but for the first twelve terms both the individual leg lengths and their sum fit comfortably in signed 64-bit integers. That is why the C++ and Java implementations can use fixed-width integer arithmetic, while Python handles the same calculation with arbitrary-precision integers.

Footnotes and References

  1. Problem page: Project Euler 138
  2. Pell equation: Wikipedia - Pell's equation
  3. Isosceles triangle: Wikipedia - Isosceles triangle
  4. Pythagorean theorem: Wikipedia - Pythagorean theorem
  5. Fibonacci number: Wikipedia - Fibonacci number

Mathematical approach · C++ solution · Python solution · Java solution

Previous: Problem 137 · All Project Euler solutions · Next: Problem 139

Need help with a problem? Ask me! 💡
e
✦ Euler GLM 5.2
Hi! I'm Euler. Ask me anything about Project Euler problems, math concepts, or solution approaches! 🧮