Problem 709: Even Stevens
View on Project EulerProject Euler Problem 709 Solution
EulerSolve provides an optimized solution for Project Euler Problem 709, Even Stevens, with C++, Python, Java, and a step-by-step mathematical explanation.
Problem Summary The required quantity for Problem 709 is the value on the right edge of the Entringer triangle at row \(n=24680\), reduced modulo \(1020202009\). Equivalently, the implementations are computing the \(n\)-th Euler zigzag number in its Entringer refinement. The main challenge is therefore not direct enumeration, but finding a recurrence that produces the same sequence with a much smaller state space. Mathematical Approach The fast method is built around the classical Entringer triangle. A small reference dynamic program can still be used for tiny sizes, but the production solver replaces the expensive binomial transition with a compact row-by-row recurrence. Step 1: The Small-Size Reference Transition For a small sanity check one may keep a state indexed by a positive integer \(r\), starting from one active state at size \(1\). When the size increases by one, the reference transition is $D_{k+1}(r')=\sum_{\substack{r\ge 1,\ 0\le t\le r \\ t\equiv 0\pmod{2} \\ r'=r-t+1}} D_k(r)\binom{r}{t}.$ The total count at size \(k\) is then $T_k=\sum_{r\ge 1} D_k(r).$ This auxiliary computation already gives the checkpoints $T_4=5,\qquad T_8=1385.$ Those are exactly the values produced by the Euler zigzag sequence at the same indices, which is the clue that the larger state space can be collapsed to Entringer numbers....
Detailed mathematical approach
Problem Summary
The required quantity for Problem 709 is the value on the right edge of the Entringer triangle at row \(n=24680\), reduced modulo \(1020202009\). Equivalently, the implementations are computing the \(n\)-th Euler zigzag number in its Entringer refinement. The main challenge is therefore not direct enumeration, but finding a recurrence that produces the same sequence with a much smaller state space.
Mathematical Approach
The fast method is built around the classical Entringer triangle. A small reference dynamic program can still be used for tiny sizes, but the production solver replaces the expensive binomial transition with a compact row-by-row recurrence.
Step 1: The Small-Size Reference Transition
For a small sanity check one may keep a state indexed by a positive integer \(r\), starting from one active state at size \(1\). When the size increases by one, the reference transition is
$D_{k+1}(r')=\sum_{\substack{r\ge 1,\ 0\le t\le r \\ t\equiv 0\pmod{2} \\ r'=r-t+1}} D_k(r)\binom{r}{t}.$
The total count at size \(k\) is then
$T_k=\sum_{r\ge 1} D_k(r).$
This auxiliary computation already gives the checkpoints
$T_4=5,\qquad T_8=1385.$
Those are exactly the values produced by the Euler zigzag sequence at the same indices, which is the clue that the larger state space can be collapsed to Entringer numbers.
Step 2: Introduce the Entringer Triangle
Define numbers \(E(r,k)\) for \(0\le k\le r\) by the boundary conditions
$E(0,0)=1,\qquad E(r,0)=0\quad (r\ge 1),$
and the recurrence
$E(r,k)=E(r,k-1)+E(r-1,r-k)\qquad (1\le k\le r).$
Each row is therefore formed by reading the previous row in reverse order and taking prefix sums. All computations are performed modulo
$M=1020202009.$
Step 3: Why the Right Edge Is the Answer
The desired value is the rightmost entry of row \(n\):
$A_n=E(n,n).$
Because the recurrence adds one reversed entry from the previous row at each step, the last entry of row \(r\) is the sum of the whole preceding row:
$E(r,r)=\sum_{j=0}^{r-1} E(r-1,j).$
This is why the right edge reproduces the Euler zigzag sequence
$1,1,1,2,5,16,61,\dots$
The full triangle refines the sequence, while the problem only asks for the final value on the right boundary.
Step 4: Worked Example
Starting from the base row
$[1],$
the recurrence generates
$\begin{aligned} r=1&:& [0,1],\\ r=2&:& [0,1,1],\\ r=3&:& [0,1,2,2],\\ r=4&:& [0,2,4,5,5]. \end{aligned}$
Hence
$E(4,4)=5,$
which matches the small verification. Continuing the same construction to row \(8\) yields
$E(8,8)=1385,$
the second checkpoint used by the implementation.
Step 5: Why the Optimized Triangle Replaces the Binomial DP
The reference transition involves many binomial coefficients and many candidate state values \(r\). The Entringer triangle compresses that information into a two-parameter table with a purely additive update. This replacement is mathematically natural because the small-size totals coincide with classical Euler zigzag values, and the Entringer triangle is the standard refinement behind that sequence. Once the identification is made, the binomial transition is only needed as a tiny checker, not as the main solver.
How the Code Works
The C++, Python, and Java implementations all follow the same optimized plan. They store only two rows of the triangle, initialize the base case \(E(0,0)=1\), and iterate from row \(1\) up to row \(n\). Inside a row, the first entry is set to \(0\), and each later entry is obtained by adding the previous entry in the current row to the mirrored entry from the previous row:
$E(r,k)=E(r,k-1)+E(r-1,r-k)\pmod{M}.$
After completing a row, the two row buffers are swapped and reused. The C++ implementation also includes a tiny reference dynamic program based on the even-binomial transition above, and it cross-checks the optimized triangle on small cases such as \(n=8\) and \(n=12\). The reported result is the final right-edge value \(E(n,n)\).
Complexity Analysis
Row \(r\) performs exactly \(r\) updates, so the total amount of work is
$\sum_{r=1}^{n} r=\frac{n(n+1)}{2}=O(n^2).$
Only two rows of length \(n+1\) are stored, so the memory usage is \(O(n)\). Each inner-loop step uses one modular addition and, when needed, one subtraction, which keeps the implementation simple and efficient.
Footnotes and References
- Problem page: Project Euler 709
- Alternating permutations: Wikipedia - Alternating permutation
- Entringer numbers and Seidel's triangle: Wikipedia - Seidel-Entringer-Arnold triangle
- Euler zigzag numbers: OEIS A000111
- Modular arithmetic: Wikipedia - Modular arithmetic