Project Euler Lab - Problem 318

#318 - 2011 Nines

● AdvancedOfficial difficulty: 40%Naive enumeration is infeasible; requires a mathematical reductionTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

Consider the real number \(\sqrt 2 + \sqrt 3\).
When we calculate the even powers of \(\sqrt 2 + \sqrt 3\) we get:
\((\sqrt 2 + \sqrt 3)^2 = 9.898979485566356 \cdots \)
\((\sqrt 2 + \sqrt 3)^4 = 97.98979485566356 \cdots \)
\((\sqrt 2 + \sqrt 3)^6 = 969.998969071069263 \cdots \)
\((\sqrt 2 + \sqrt 3)^8 = 9601.99989585502907 \cdots \)
\((\sqrt 2 + \sqrt 3)^{10} = 95049.999989479221 \cdots \)
\((\sqrt 2 + \sqrt 3)^{12} = 940897.9999989371855 \cdots \)
\((\sqrt 2 + \sqrt 3)^{14} = 9313929.99999989263 \cdots \)
\((\sqrt 2 + \sqrt 3)^{16} = 92198401.99999998915 \cdots \)

It looks as if the number of consecutive nines at the beginning of the fractional part of these powers is non-decreasing.
In fact it can be proven that the fractional part of \((\sqrt 2 + \sqrt 3)^{2 n}\) approaches \(1\) for large \(n\).

Consider all real numbers of the form \(\sqrt p + \sqrt q\) with \(p\) and \(q\) positive integers and \(p < q\), such that the fractional part of \((\sqrt p + \sqrt q)^{ 2 n}\) approaches \(1\) for large \(n\).

Let \(C(p,q,n)\) be the number of consecutive nines at the beginning of the fractional part of \((\sqrt p + \sqrt q)^{ 2 n}\).

Let \(N(p,q)\) be the minimal value of \(n\) such that \(C(p,q,n) \ge 2011\).

Find \(\displaystyle \sum N(p,q) \,\, \text{ for } p+q \le 2011\).

This problem is taken from Project Euler, Problem 318.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=318. Published Saturday, 1st January 2011, 04:00 pm. Solved by 1,124 members at time of mirroring.

Why this is useful

Optimization. The transferable skill is replacing infeasible enumeration with a mathematical reduction - the core move in calibration and large-scale computation (Phases 10, 13).

We classify relevance honestly - not every Euler problem is a trading application.

Prerequisites

Lessons that prepare you:
19.14 Computational Complexity, Feasibility Estimation, and Proving Algorithms Correct · 19.4 Exact Arithmetic: Big Integers, Rationals, and Floating-Point Traps

Recommended stepping-stone problems: #731 · #169 · #455

Concepts: brute-force-reduction

Likely techniques: exact-rational

Learning mode

Pick how much scaffolding you want. Your choice is remembered per problem.

Scratchpad

Mathematical notes, formulas, pseudocode, hypotheses, complexity notes. Saved automatically with your progress.

Python workbench

Tier C - reduced scale in browser; full scale in notebook
Browser runs a reduced, clearly-labelled educational scale; the original scale is provided in a local notebook.

Real Python (Pyodide) in a sandboxed Web Worker - no network, no filesystem, no DOM access. Ctrl/Cmd+Enter runs. Escape leaves the editor. Stop terminates the worker.

Python runtime not loaded (it boots on first run - a one-time local load).

Check your answer

Answers are checked against a salted hash held in a separate file - not printed in this page. This prevents accidental spoilers; it is not cryptographic protection (see the build notes).

Progressive hints

Confidence

Low confidence schedules this problem for spaced review, even if you solved it.

Reference solution

Spoiler
The complete original explanation (interpretation, naive approach, insight, proof, complexity, Python implementation, tests, common mistakes, alternatives) is hidden and lazy-loaded. Reveal it only after a meaningful attempt - the struggle is where the learning happens.