Open Source · 7 Languages · 900+ Problems

Project Euler Solutions

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

All Problems

Problem 405: A Rectangular Tiling

View on Project Euler

Project Euler Problem 405 Solution

EulerSolve provides an optimized solution for Project Euler Problem 405, A Rectangular Tiling, with C++, Python, Java, and a step-by-step mathematical explanation.

Problem Summary Let \(f(n)\) denote the tiling count defined by the problem. The key fact used by the implementation is that this counting sequence already has an exact closed form, so the task is no longer to enumerate tilings but to evaluate that formula at an enormous index. The program is written in terms of \(f(10^k)\), and the actual Project Euler instance uses \(k=10^{18}\), so the target quantity is \(f(10^{10^{18}})\bmod 17^7\). Mathematical Approach Step 1: Start from the closed form The C++, Python, and Java implementations all begin with the identity $f(n)=1-\frac{(-1)^n}{15}-\frac{4}{3}2^n+\frac{2}{5}4^n.$ This means the original tiling combinatorics have been compressed into three exponential terms and a constant. Once this formula is known, the whole problem becomes modular arithmetic with very large exponents. Step 2: Why the formula still gives integers Although the expression contains fractions, the result is always an integer. Multiply both sides by \(15\): $15f(n)=15-(-1)^n-20\cdot 2^n+6\cdot 4^n.$ The right-hand side is divisible by \(3\) and by \(5\)....

Detailed mathematical approach

Problem Summary

Let \(f(n)\) denote the tiling count defined by the problem. The key fact used by the implementation is that this counting sequence already has an exact closed form, so the task is no longer to enumerate tilings but to evaluate that formula at an enormous index. The program is written in terms of \(f(10^k)\), and the actual Project Euler instance uses \(k=10^{18}\), so the target quantity is \(f(10^{10^{18}})\bmod 17^7\).

Mathematical Approach

Step 1: Start from the closed form

The C++, Python, and Java implementations all begin with the identity

$f(n)=1-\frac{(-1)^n}{15}-\frac{4}{3}2^n+\frac{2}{5}4^n.$

This means the original tiling combinatorics have been compressed into three exponential terms and a constant. Once this formula is known, the whole problem becomes modular arithmetic with very large exponents.

Step 2: Why the formula still gives integers

Although the expression contains fractions, the result is always an integer. Multiply both sides by \(15\):

$15f(n)=15-(-1)^n-20\cdot 2^n+6\cdot 4^n.$

The right-hand side is divisible by \(3\) and by \(5\). Modulo \(3\), we use \(2\equiv -1\pmod 3\), so

$-(-1)^n-20\cdot 2^n\equiv -(-1)^n-2^{n+1}\equiv -(-1)^n-(-1)^{n+1}\equiv 0\pmod 3.$

Modulo \(5\), we use \(4\equiv -1\pmod 5\), so

$-(-1)^n+6\cdot 4^n\equiv -(-1)^n+4^n\equiv -(-1)^n+(-1)^n\equiv 0\pmod 5.$

Hence the numerator is always divisible by \(15\), which explains why the sequence values are integers even though the closed form is written with rational coefficients.

Step 3: Move the formula into modular arithmetic

Let

$M=17^7=410338673.$

Because \(3\), \(5\), and \(15\) are all coprime to \(M\), they have modular inverses. Therefore the same formula can be evaluated modulo \(M\) as

$f(n)\equiv 1-(-1)^n\cdot 15^{-1}-4\cdot 2^n\cdot 3^{-1}+2\cdot 4^n\cdot 5^{-1}\pmod M.$

This is the exact form used by the implementation: the fractions are replaced by modular inverses, and every multiplication is reduced modulo \(M\).

Step 4: Reduce the gigantic exponent

The hard part is that \(n\) itself is huge. In the actual instance, \(n=10^{10^{18}}\). Since \(2\) and \(4\) are both coprime to \(17\), Euler's theorem applies. For a prime power \(17^7\),

$\varphi(M)=17^7-17^6=16\cdot 17^6=386201104.$

Hence

$2^{\varphi(M)}\equiv 1\pmod M,\qquad 4^{\varphi(M)}\equiv 1\pmod M,$

so exponents may be reduced modulo \(\varphi(M)\):

$2^n\equiv 2^{\,n\bmod \varphi(M)}\pmod M,\qquad 4^n\equiv 4^{\,n\bmod \varphi(M)}\pmod M.$

For the parameterized form \(n=10^k\), we therefore compute

$e\equiv 10^k\pmod{\varphi(M)}$

and then evaluate only \(2^e\) and \(4^e\) modulo \(M\). For the actual input \(k=10^{18}\), this reduction gives

$10^{10^{18}}\equiv 152370032\pmod{386201104}.$

So the astronomically large exponents collapse to ordinary modular powers with exponent \(152370032\).

Step 5: Use the parity of \(10^k\)

The sign term is even simpler. If \(k\ge 1\), then \(10^k\) is even, so

$(-1)^{10^k}=1.$

Only the degenerate case \(k=0\) gives \(n=1\), where the sign is negative. Therefore, for every real problem instance with \(k\ge 1\), the formula simplifies to

$f(10^k)\equiv 1-15^{-1}-4\cdot 2^e\cdot 3^{-1}+2\cdot 4^e\cdot 5^{-1}\pmod M,$

with \(e\equiv 10^k\pmod{\varphi(M)}\).

Worked Example

A small exact check is \(n=4\). Then

$f(4)=1-\frac{1}{15}-\frac{4}{3}\cdot 16+\frac{2}{5}\cdot 256.$

Putting everything over denominator \(15\),

$f(4)=\frac{15-1-320+1536}{15}=\frac{1230}{15}=82.$

This matches the checkpoint used in the implementation. Another useful consistency check is

$f(10^9)\equiv 126897180\pmod{17^7}.$

How the Code Works

The implementation performs only a constant number of fast modular exponentiations. First it computes \(\varphi(17^7)\). Next it finds \(10^k \bmod \varphi(17^7)\) by binary exponentiation, which is enough to recover the needed powers of \(2\) and \(4\). The rational coefficients are converted into modular inverses using Euler's theorem in the form \(a^{-1}\equiv a^{\varphi(M)-1}\pmod M\) for \(a\in\{3,5,15\}\). Finally the four terms of the closed form are assembled with modular addition and subtraction.

So the program never constructs \(10^{10^{18}}\) as an ordinary integer, never enumerates tilings, and never performs any dynamic programming over the combinatorial objects. Everything is reduced to a tiny number of modular operations.

Complexity Analysis

Binary exponentiation takes logarithmic time in the exponent. Therefore the full method costs

$O(\log k+\log M)$

time and \(O(1)\) memory. Since the modulus \(M=17^7\) is fixed in this problem, the practical cost is essentially \(O(\log k)\) with constant auxiliary space.

Footnotes and References

  1. Problem page: https://projecteuler.net/problem=405
  2. Euler's theorem: Wikipedia - Euler's theorem
  3. Modular multiplicative inverse: Wikipedia - Modular multiplicative inverse
  4. Modular exponentiation: Wikipedia - Modular exponentiation
  5. Euler totient function: Wikipedia - Euler's totient function

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

Previous: Problem 404 · All Project Euler solutions · Next: Problem 406

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! 🧮