All Euler problems
Project Euler

2011 Nines

For integers 1 <= p < q with p + q <= 2011, let C(p,q,n) be the count of consecutive nines at the start of the fractional part of (sqrt(p) + sqrt(q))^2n. Let N(p,q) be the minimal n with C(p,q,n) >...

Source sync Apr 19, 2026
Problem #0318
Level Level 14
Solved By 1,103
Languages C++, Python
Answer 709313889
Length 267 words
algebrageometrybrute_force

Problem Statement

This archive keeps the full statement, math, and original media on the page.

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\).

Problem 318: 2011 Nines

Mathematical Foundation

Lemma 1 (Conjugate Pair). Define α=(p+q)2=p+q+2pq\alpha = (\sqrt{p}+\sqrt{q})^2 = p+q+2\sqrt{pq} and β=(pq)2=p+q2pq\beta = (\sqrt{p}-\sqrt{q})^2 = p+q-2\sqrt{pq}. Then α\alpha and β\beta are roots of:

x22(p+q)x+(pq)2=0.x^2 - 2(p+q)\,x + (p-q)^2 = 0.

Proof. We have α+β=2(p+q)\alpha + \beta = 2(p+q) and αβ=(p+q)24pq=(pq)2\alpha\beta = (p+q)^2 - 4pq = (p-q)^2, both integers. The quadratic with roots α,β\alpha, \beta is x2(α+β)x+αβ=0x^2 - (\alpha+\beta)x + \alpha\beta = 0. \square

Theorem 1 (Integer Power Sums). For all n0n \ge 0, Sn:=αn+βnZ+S_n := \alpha^n + \beta^n \in \mathbb{Z}^+.

Proof. By Newton’s identity, the power sums of roots of a monic polynomial with integer coefficients are integers. Concretely, SnS_n satisfies the recurrence:

S0=2,S1=2(p+q),Sn=2(p+q)Sn1(pq)2Sn2.S_0 = 2, \quad S_1 = 2(p+q), \quad S_n = 2(p+q)\,S_{n-1} - (p-q)^2\,S_{n-2}.

By induction, each SnZS_n \in \mathbb{Z}. Since α>0\alpha > 0 and β0\beta \ge 0, we have Sn>0S_n > 0. \square

Lemma 2 (Finiteness Condition). N(p,q)N(p,q) is finite if and only if:

  1. 0<β<10 < \beta < 1, equivalently 0<qp<10 < |\sqrt{q} - \sqrt{p}| < 1, equivalently q<(p+1)2q < (\sqrt{p}+1)^2, and
  2. pqpq is not a perfect square (so βZ\beta \notin \mathbb{Z}).

Proof. If pqpq is a perfect square, then pqZ\sqrt{pq} \in \mathbb{Z}, so α,βZ\alpha, \beta \in \mathbb{Z} and αn\alpha^n is itself an integer (no fractional nines). If pqpq is not a perfect square and 0<β<10 < \beta < 1, then αn=Snβn\alpha^n = S_n - \beta^n where SnZS_n \in \mathbb{Z} and 0<βn<10 < \beta^n < 1, so {αn}=1βn1\{\alpha^n\} = 1 - \beta^n \to 1, giving arbitrarily many nines. If β1\beta \ge 1 or β=0\beta = 0, the fractional part does not converge to 1. \square

Theorem 2 (Counting Consecutive Nines). When 0<β<10 < \beta < 1 and βZ\beta \notin \mathbb{Z}:

C(p,q,n)=nlog10β.C(p,q,n) = \lfloor -n \log_{10}\beta \rfloor.

Proof. The fractional part is {αn}=1βn\{\alpha^n\} = 1 - \beta^n. The number of leading nines in a decimal 0.9990.999\ldots equals log10(10.999)\lfloor -\log_{10}(1 - 0.999\ldots) \rfloor. More precisely, C(p,q,n)=log10(βn)=nlog10βC(p,q,n) = \lfloor -\log_{10}(\beta^n) \rfloor = \lfloor -n\log_{10}\beta \rfloor, since 1{αn}=βn1 - \{\alpha^n\} = \beta^n and the leading nines count is log10βn\lfloor -\log_{10}\beta^n \rfloor. \square

Corollary (Formula for N(p,q)N(p,q)). The minimal nn with C(p,q,n)2011C(p,q,n) \ge 2011 is:

N(p,q)=2011log10β=2011log10(p+q2pq).N(p,q) = \left\lceil \frac{2011}{-\log_{10}\beta} \right\rceil = \left\lceil \frac{-2011}{\log_{10}(p+q-2\sqrt{pq})} \right\rceil.

Proof. C(p,q,n)2011C(p,q,n) \ge 2011 iff nlog10β2011-n\log_{10}\beta \ge 2011 iff n2011/(log10β)n \ge 2011/(-\log_{10}\beta). The smallest such integer nn is the ceiling. \square

Editorial

For 1 <= p < q with p+q <= 2011, where pq is not a perfect square and |sqrt(q) - sqrt(p)| < 1: beta = (sqrt(p) - sqrt(q))^2 = p + q - 2*sqrt(pq) N(p,q) = ceil(-2011 / log10(beta)) Find the sum of N(p,q) over all valid pairs. We return total.

Pseudocode

Input: bound = 2011, target = 2011
Output: sum of N(p,q) over valid pairs
total = 0
For p = 1 to 1005:
Return total

Complexity Analysis

  • Time: O ⁣(p=11005Δp)O\!\left(\sum_{p=1}^{1005} \Delta_p\right) where Δp=(p+1)2p2p\Delta_p = \lfloor(\sqrt{p}+1)^2\rfloor - p \approx 2\sqrt{p}. Total: O ⁣(p=110052p)=O(P3/2)O(104.5)O\!\left(\sum_{p=1}^{1005} 2\sqrt{p}\right) = O(P^{3/2}) \approx O(10^{4.5}), negligible.
  • Space: O(1)O(1).

Answer

709313889\boxed{709313889}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_318/solution.cpp
#include <bits/stdc++.h>
using namespace std;

/*
 * Problem 318: 2011 Nines
 *
 * For p < q with p+q <= 2011, pq not a perfect square, and (sqrt(q)-sqrt(p)) < 1:
 *   beta = p + q - 2*sqrt(pq)
 *   N(p,q) = ceil(-2011 / log10(beta))
 *
 * Sum all N(p,q).
 */

int main(){
    const int L = 2011;
    long long total = 0;

    for(int p = 1; p < L; p++){
        double sp = sqrt((double)p);
        int max_q = min(L - p, (int)((sp + 1.0) * (sp + 1.0)) + 1);

        for(int q = p + 1; q <= max_q; q++){
            if(p + q > L) break;

            // Check if pq is a perfect square
            long long pq = (long long)p * q;
            long long sq = (long long)sqrt((double)pq);
            // Adjust for floating point
            while(sq * sq > pq) sq--;
            while((sq + 1) * (sq + 1) <= pq) sq++;
            if(sq * sq == pq) continue;

            double beta = (double)p + (double)q - 2.0 * sqrt((double)pq);
            if(beta <= 0.0 || beta >= 1.0) continue;

            double n = -2011.0 / log10(beta);
            long long N = (long long)ceil(n - 1e-9); // ceil with small tolerance
            // More careful: use exact ceil
            N = (long long)ceil(n);
            // Handle floating point: if N * (-log10(beta)) is very close to 2011
            // we need to be careful. For safety:
            if((double)N * (-log10(beta)) < 2011.0 - 1e-9) N++;

            total += N;
        }
    }

    cout << total << endl;
    return 0;
}