Problem 835: Supernatural Triangles
View on Project EulerProject Euler Problem 835 Solution
EulerSolve provides an optimized solution for Project Euler Problem 835, Supernatural Triangles, with C++, Python, Java, and a step-by-step mathematical explanation.
Problem Summary The contributing triangles split into two infinite Pythagorean families. One family has hypotenuse exactly one larger than a leg, and the other has two consecutive legs. The task is to sum every distinct perimeter not exceeding \(N=10^{10^{10}}\) and return the result modulo \(1234567891\). Because \(N\) is unimaginably large, the solution replaces any geometric search with closed formulas, a Pell-type recurrence, and one overlap correction. Mathematical Approach Let \(S(N)\) be the required perimeter sum, and let \(M=1234567891\) be the modulus used at the end. Step 1: First family from Euclid's parametrization For a primitive right triangle we may write $a=m^2-n^2,\qquad b=2mn,\qquad c=m^2+n^2,\qquad m>n\ge 1.$ Impose the condition that the hypotenuse is one larger than one leg: $c=b+1.$ Then $m^2+n^2=2mn+1 \Longrightarrow (m-n)^2=1,$ so necessarily \(m=n+1\). Substituting gives $a=2n+1,\qquad b=2n(n+1),\qquad c=2n(n+1)+1.$ If we set \(t=2n+1\), then \(t\) is odd and the perimeter becomes $p=t+\frac{t^2-1}{2}+\frac{t^2+1}{2}=t(t+1).$ So Family I contributes exactly the perimeters \(t(t+1)\) with odd \(t\ge 3\). Step 2: Closed-form sum for Family I The inequality \(t(t+1)\le N\) implies \(t<\sqrt{N}\)....
Detailed mathematical approach
Problem Summary
The contributing triangles split into two infinite Pythagorean families. One family has hypotenuse exactly one larger than a leg, and the other has two consecutive legs. The task is to sum every distinct perimeter not exceeding \(N=10^{10^{10}}\) and return the result modulo \(1234567891\). Because \(N\) is unimaginably large, the solution replaces any geometric search with closed formulas, a Pell-type recurrence, and one overlap correction.
Mathematical Approach
Let \(S(N)\) be the required perimeter sum, and let \(M=1234567891\) be the modulus used at the end.
Step 1: First family from Euclid's parametrization
For a primitive right triangle we may write
$a=m^2-n^2,\qquad b=2mn,\qquad c=m^2+n^2,\qquad m>n\ge 1.$
Impose the condition that the hypotenuse is one larger than one leg:
$c=b+1.$
Then
$m^2+n^2=2mn+1 \Longrightarrow (m-n)^2=1,$
so necessarily \(m=n+1\). Substituting gives
$a=2n+1,\qquad b=2n(n+1),\qquad c=2n(n+1)+1.$
If we set \(t=2n+1\), then \(t\) is odd and the perimeter becomes
$p=t+\frac{t^2-1}{2}+\frac{t^2+1}{2}=t(t+1).$
So Family I contributes exactly the perimeters \(t(t+1)\) with odd \(t\ge 3\).
Step 2: Closed-form sum for Family I
The inequality \(t(t+1)\le N\) implies \(t<\sqrt{N}\). Here \(N=10^E\) with \(E=10^{10}\), and \(E\) is even, so
$\sqrt{N}=10^{E/2}.$
Write
$s=10^{E/2},\qquad q=\frac{s}{2}.$
The odd values below \(s\) are \(1,3,\dots,s-1\). Using \(t=2j-1\),
$\sum_{j=1}^{q}(2j-1)(2j)=\sum_{j=1}^{q}(4j^2-2j)=\frac{q(q+1)(4q-1)}{3}.$
The term \(t=1\) corresponds to the degenerate triple \((1,0,1)\) with perimeter \(2\), so the valid Family I contribution is
$S_I(N)=\frac{q(q+1)(4q-1)}{3}-2.$
Step 3: Second family from consecutive legs
Now look at right triangles whose legs are consecutive, say \(x\) and \(x+1\), with hypotenuse \(c\). Then
$x^2+(x+1)^2=c^2.$
Introduce
$u=2x+1.$
Since \(u^2=4x^2+4x+1\), the Pythagorean condition becomes the negative Pell equation
$u^2-2c^2=-1.$
Its positive solutions are generated by multiplication with \(3+2\sqrt{2}\). Starting from \(7+5\sqrt{2}\), which corresponds to the triangle \((3,4,5)\), we get
$u_{k+1}=3u_k+4c_k,\qquad c_{k+1}=2u_k+3c_k.$
The perimeter of such a triangle is
$p_k=u_k+c_k,$
so the first values are
$12,\ 70,\ 408,\ 2378,\dots$
Step 4: Linear recurrence for the perimeter sequence
The Pell multiplier has conjugate roots
$\lambda=3+2\sqrt{2},\qquad \mu=3-2\sqrt{2}.$
They satisfy \(\lambda+\mu=6\) and \(\lambda\mu=1\), so any sequence built from these Pell solutions satisfies
$r^2-6r+1=0.$
In particular, the perimeter sequence obeys
$p_{k+1}=6p_k-p_{k-1},\qquad p_1=12,\qquad p_2=70.$
If \(K\) is the largest index with \(p_K\le N\), then Family II contributes
$S_{II}(N)=\sum_{k=1}^{K}p_k.$
Step 5: Find the cutoff index without iterating to \(N\)
The recurrence has closed form
$p_k=\alpha \lambda^k+\beta \mu^k,$
with \(|\mu|<1\). Therefore \(p_k\) grows like \(\alpha \lambda^k\), and an accurate first estimate is
$K\approx \frac{\log_{10}N-\log_{10}\alpha}{\log_{10}\lambda}.$
After that estimate is computed, a few monotone comparisons are enough to adjust \(K\) until \(p_K\le N<p_{K+1}\). This avoids building huge integers such as \(10^{10^{10}}\) explicitly.
Step 6: Combine both families and remove the overlap
The only perimeter counted twice is \(12\), coming from the triangle \((3,4,5)\). In Family I the sides are
$t,\qquad \frac{t^2-1}{2},\qquad \frac{t^2+1}{2}.$
To also have consecutive legs we need
$\frac{t^2-1}{2}=t+1 \Longrightarrow t^2-2t-3=0 \Longrightarrow t=3.$
Hence the final formula is
$S(N)=S_I(N)+S_{II}(N)-12 \pmod{1234567891}.$
Worked Example: \(N=100\)
Family I contributes the perimeters \(12,30,56,90\), whose sum is \(188\).
Family II contributes \(12,70\), whose sum is \(82\).
Subtract the shared perimeter \(12\) once:
$S(100)=188+82-12=258.$
This matches the checkpoint used by the implementations.
How the Code Works
The C++, Python, and Java implementations evaluate Family I entirely modulo \(1234567891\). They compute \(10^{E/2}\bmod M\), replace division by \(2\) and \(3\) with modular inverses, and apply the closed formula for \(S_I(N)\) directly.
For Family II, the implementation first estimates the largest admissible index from the dominant root \(3+2\sqrt{2}\), then corrects the index with a few monotone checks. The sum of the recurrence is obtained with fast exponentiation of a \(3\times 3\) transition matrix whose state stores the current perimeter, the previous perimeter, and the running total.
Finally the two partial sums are added modulo \(1234567891\), and the overlap \(12\) is removed exactly once.
Complexity Analysis
Family I is handled in \(O(1)\) modular arithmetic. Family II uses \(O(\log K)\) multiplications of fixed \(3\times 3\) matrices, plus constant-time index correction. Memory usage is \(O(1)\). The running time depends only on the recurrence index, not on the astronomical size of \(N\).
Footnotes and References
- Problem page: Project Euler 835
- Pythagorean triples: Wikipedia - Pythagorean triple
- Pell-type equations: Wikipedia - Pell's equation
- Recurrence relations: Wikipedia - Recurrence relation
- Matrix exponentiation for linear recurrences: cp-algorithms - Fibonacci numbers and matrix exponentiation