Open Source · 7 Languages · 1007 Problems + Bonus

Project Euler Solutions

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

All Problems

Problem 318: 2011 Nines

View on Project Euler

Project Euler Problem 318 Solution

EulerSolve provides an optimized solution for Project Euler Problem 318, 2011 Nines, with C++, Python, Java, and a step-by-step mathematical explanation.

Problem Summary For every pair of positive integers $$1\le p \lt q,\qquad p+q\le 2011,$$ consider $$\alpha=\sqrt p+\sqrt q.$$ We look at the even powers \(\alpha^{2n}\), and \(C(p,q,n)\) is the number of consecutive 9s at the beginning of the fractional part of \(\alpha^{2n}\). Let \(N(p,q)\) be the smallest \(n\) such that $$C(p,q,n)\ge 2011.$$ The goal is to compute $$\sum_{p+q\le 2011} N(p,q),$$ but only for those pairs where the fractional part of \(\alpha^{2n}\) approaches \(1\). Mathematical Approach 1) Introduce the conjugate partner. Set $$\beta=\sqrt q-\sqrt p.$$ Then $$\alpha^{2n}+\beta^{2n}$$ is always an integer. The reason is that in the binomial expansions of \((\sqrt q+\sqrt p)^{2n}\) and \((\sqrt q-\sqrt p)^{2n}\), all odd-radical terms cancel and only integer terms remain. So if we define $$A_n=\alpha^{2n}+\beta^{2n}\in\mathbb Z,$$ then $$\alpha^{2n}=A_n-\beta^{2n}.$$ 2) Exactly when does the fractional part approach \(1\)? Because \(p \lt q\), we have \(\beta \gt 0\). The fractional part of \(\alpha^{2n}\) approaches \(1\) exactly when \(\beta^{2n}\to0\), i.e. $$0\lt \beta \lt 1.$$ If \(\beta\ge1\), then \(\beta^{2n}\) does not decay to \(0\), so the fractional part cannot converge to \(1\). Thus the admissible pairs are precisely those with $$\sqrt q-\sqrt p \lt 1.$$ 3) Fractional part as \(1-\beta^{2n}\)....

Detailed mathematical approach

Problem Summary

For every pair of positive integers

$$1\le p \lt q,\qquad p+q\le 2011,$$

consider

$$\alpha=\sqrt p+\sqrt q.$$

We look at the even powers \(\alpha^{2n}\), and \(C(p,q,n)\) is the number of consecutive 9s at the beginning of the fractional part of \(\alpha^{2n}\).

Let \(N(p,q)\) be the smallest \(n\) such that

$$C(p,q,n)\ge 2011.$$

The goal is to compute

$$\sum_{p+q\le 2011} N(p,q),$$

but only for those pairs where the fractional part of \(\alpha^{2n}\) approaches \(1\).

Mathematical Approach

1) Introduce the conjugate partner.

Set

$$\beta=\sqrt q-\sqrt p.$$

Then

$$\alpha^{2n}+\beta^{2n}$$

is always an integer. The reason is that in the binomial expansions of \((\sqrt q+\sqrt p)^{2n}\) and \((\sqrt q-\sqrt p)^{2n}\), all odd-radical terms cancel and only integer terms remain.

So if we define

$$A_n=\alpha^{2n}+\beta^{2n}\in\mathbb Z,$$

then

$$\alpha^{2n}=A_n-\beta^{2n}.$$

2) Exactly when does the fractional part approach \(1\)?

Because \(p \lt q\), we have \(\beta \gt 0\). The fractional part of \(\alpha^{2n}\) approaches \(1\) exactly when \(\beta^{2n}\to0\), i.e.

$$0\lt \beta \lt 1.$$

If \(\beta\ge1\), then \(\beta^{2n}\) does not decay to \(0\), so the fractional part cannot converge to \(1\).

Thus the admissible pairs are precisely those with

$$\sqrt q-\sqrt p \lt 1.$$

3) Fractional part as \(1-\beta^{2n}\).

For every admissible pair we have \(0\lt \beta^{2n}\lt1\), hence

$$\alpha^{2n}=A_n-\beta^{2n}$$

lies just below the integer \(A_n\). Therefore

$$\{\alpha^{2n}\}=1-\beta^{2n}.$$

This is the whole reason the problem becomes easy: the complicated-looking irrational power is controlled by the tiny positive quantity \(\beta^{2n}\).

4) Translate "at least \(K\) leading nines".

Let \(x=\{\alpha^{2n}\}\). The fractional part begins with at least \(K\) nines if and only if

$$x \gt 1-10^{-K}.$$

Since \(x=1-\beta^{2n}\), this is equivalent to

$$\beta^{2n}\lt 10^{-K}.$$

Here \(K=2011\) for the actual problem.

5) Take logarithms.

Because \(0\lt \beta^2 \lt 1\), the quantity

$$\lambda=-\log_{10}(\beta^2)$$

is positive. The inequality above becomes

$$n\lambda \gt K.$$

So the minimal valid exponent is

$$N(p,q)=\left\lceil\frac{K}{\lambda}\right\rceil=\left\lceil\frac{K}{-\log_{10}(\beta^2)}\right\rceil.$$

This is exactly what the C++ function minimal_n computes, with a tiny tolerance to avoid floating-point boundary mistakes.

6) Worked example: \((p,q)=(2,3)\).

Here

$$\beta=\sqrt3-\sqrt2\approx0.3178372452,$$

so

$$\beta^2\approx0.1010205144,$$

and

$$\lambda=-\log_{10}(\beta^2)\approx0.9955904242.$$

For one leading 9 we need \(K=1\), hence

$$N(2,3)=\left\lceil\frac{1}{0.9955904242}\right\rceil=2.$$

That matches the sequence shown in the statement:

\((\sqrt2+\sqrt3)^2=9.8989\ldots\) has no leading 9 in the fractional part,

\((\sqrt2+\sqrt3)^4=97.9897\ldots\) has one,

\((\sqrt2+\sqrt3)^6=969.9989\ldots\) has two,

\((\sqrt2+\sqrt3)^8=9601.9998\ldots\) has three.

So the checkpoints

$$N(2,3;K=1)=2,\qquad N(2,3;K=2)=3,\qquad N(2,3;K=3)=4$$

are exactly correct.

7) Small full example: \(M=5,\ K=1\).

The admissible pairs are

$$ (1,2),\ (1,3),\ (2,3). $$

Their minimal values are

$$N(1,2)=2,\qquad N(1,3)=4,\qquad N(2,3)=2.$$

Therefore

$$S(5,1)=2+4+2=8,$$

which is the second checkpoint in the source code.

8) Final summation formula.

The complete answer is

$$\sum_{\substack{1\le p \lt q\\ p+q\le 2011\\ \sqrt q-\sqrt p\lt1}} \left\lceil\frac{2011}{-\log_{10}\!\left((\sqrt q-\sqrt p)^2\right)}\right\rceil.$$

No deeper number theory is needed after this reduction; the implementation simply iterates over all candidate pairs.

Algorithm

1) Loop over all pairs \((p,q)\) with \(1\le p \lt q\) and \(p+q\le M\).

2) Compute

$$\beta=\sqrt q-\sqrt p.$$

3) Skip the pair if \(\beta\ge1\).

4) Compute \(\lambda=-\log_{10}(\beta^2)\).

5) Add

$$\left\lceil\frac{K}{\lambda}\right\rceil$$

to the running total.

Complexity Analysis

The triangular region \(p+q\le M\) contains

$$O(M^2)$$

pairs. Each pair needs a constant amount of work: two square roots, one logarithm, and a few arithmetic operations. So the total complexity is

$$O(M^2)$$

time and

$$O(1)$$

memory.

Checks And Final Result

The implementation checks

$$N(2,3;1)=2,\qquad N(2,3;2)=3,\qquad N(2,3;3)=4,$$

and

$$S(5,1)=8.$$

For the full problem \((M,K)=(2011,2011)\), the final answer is

$$709313889.$$

Further Reading

  1. Problem page: https://projecteuler.net/problem=318
  2. Logarithm: https://en.wikipedia.org/wiki/Logarithm
  3. Floating-point arithmetic: https://en.wikipedia.org/wiki/Floating-point_arithmetic

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

Previous: Problem 317 · All Project Euler solutions · Next: Problem 319