All Euler problems
Project Euler

A Stoneham Number

Define the Stoneham number: A = sum_(i=1)^infinity frac13^i * 10^(3^i). Let A(n) denote the 10 decimal digits starting from position n in the decimal expansion of A. Given: A(100) = 4938271604, A(1...

Source sync Apr 19, 2026
Problem #0731
Level Level 19
Solved By 698
Languages C++, Python
Answer 6086371427
Length 333 words
modular_arithmeticgeometrylinear_algebra

Problem Statement

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

\[A=\displaystyle \sum _{i=1}^{\infty } \frac {1}{3^i 10^{3^i}}\] Define \(A(n)\) to be the \(10\) decimal digits from the \(n\)th digit onward. For example, \(A(100) = 4938271604\) and \(A(10^8)=2584642393\).

Find \(A(10^{16})\).

Problem 731: A Stoneham Number

Mathematical Foundation

Theorem 1 (Isolated Term Structure). For all i1i \ge 1, the ii-th term Ti=13i103iT_i = \frac{1}{3^i \cdot 10^{3^i}} contributes nonzero digits only at decimal positions 3i+1\ge 3^i + 1. Moreover, for i2i \ge 2, the digit blocks of TiT_i and Ti+1T_{i+1} do not overlap in positions [3i+1,3i+1][3^i + 1,\, 3^{i+1}].

Proof. The term Ti=103i3iT_i = 10^{-3^i} \cdot 3^{-i} has its first nonzero decimal digit at position 3i+13^i + 1 (the factor 103i10^{-3^i} shifts 1/3i1/3^i rightward by 3i3^i places). The decimal expansion of 1/3i1/3^i is purely periodic with period ord3i(10)\operatorname{ord}_{3^i}(10), which divides ϕ(3i)=23i1\phi(3^i) = 2 \cdot 3^{i-1}. Hence TiT_i occupies digits in the range [3i+1,3i+23i1]=[3i+1,3i+23i1][3^i+1, \, 3^i + 2 \cdot 3^{i-1}] = [3^i+1,\, 3^i + 2\cdot 3^{i-1}] before repeating. Since 3i+23i1=3i1(3+2)=53i1<3i+1=33i3^i + 2 \cdot 3^{i-1} = 3^{i-1}(3+2) = 5 \cdot 3^{i-1} < 3^{i+1} = 3 \cdot 3^i for all i1i \ge 1, the repeating block of TiT_i fits entirely before position 3i+1+13^{i+1}+1 where Ti+1T_{i+1} begins. Thus the blocks are isolated. \square

Lemma 1 (Digit Extraction). The jj-th decimal digit (after the decimal point) of 1/3i1/3^i is

dj=10rj3i,where rj=10j1mod3i,d_j = \left\lfloor \frac{10 \cdot r_j}{3^i} \right\rfloor, \quad \text{where } r_j = 10^{j-1} \bmod 3^i,

and rjr_j is computable in O(logj)O(\log j) arithmetic operations via modular exponentiation.

Proof. Write 1/3i=0.d1d2d31/3^i = 0.d_1 d_2 d_3 \cdots. By the standard long-division algorithm, r1=1r_1 = 1 and rj+1=10rjmod3ir_{j+1} = 10 \, r_j \bmod 3^i, with dj=10rj/3id_j = \lfloor 10\, r_j / 3^i \rfloor. By induction, rj=10j1mod3ir_j = 10^{j-1} \bmod 3^i. Computing 10j1mod3i10^{j-1} \bmod 3^i uses repeated squaring in O(logj)O(\log j) multiplications modulo 3i3^i. \square

Theorem 2 (Digit Block Summation). The 10-digit block A(n)A(n) equals the last 10 digits of

i13in+9Bi(n),\sum_{\substack{i \ge 1 \\ 3^i \le n+9}} B_i(n),

where Bi(n)B_i(n) is the integer formed by the 10 digits of TiT_i at positions nn through n+9n+9, and carries are propagated from right to left.

Proof. Since A=i=1TiA = \sum_{i=1}^{\infty} T_i and the digit blocks are isolated (Theorem 1), only finitely many terms TiT_i contribute nonzero digits at any given position. Specifically, term TiT_i can only contribute at position nn if 3in+93^i \le n + 9 (otherwise TiT_i‘s digits start after position n+9n+9). The 10-digit block of TiT_i at position nn is determined by extracting digits n3in - 3^i through n3i+9n - 3^i + 9 of 1/3i1/3^i via Lemma 1. Summing these integer blocks and propagating carries yields A(n)A(n). \square

Editorial

A = sum_{i=1}^{inf} 1/(3^i * 10^{3^i}) A(n) = 10 digits of A starting at position n. Key: digits at position n come from terms with 3^i <= n+9. For each term, compute digits of 1/3^i at offset n - 3^i using modular exponentiation. We find contributing terms. We then compute 10-digit block for each contributing term. Finally, iterate over i in terms.

Pseudocode

Find contributing terms
Compute 10-digit block for each contributing term
for i in terms
Extract last 10 digits (handle carries)

Complexity Analysis

  • Time: O(log3nlogn)O(\log_3 n \cdot \log n). There are O(log3n)O(\log_3 n) contributing terms, each requiring O(1)O(1) modular exponentiations of cost O(logn)O(\log n).
  • Space: O(logn)O(\log n) for storing intermediate values in modular exponentiation.

Answer

6086371427\boxed{6086371427}

Code

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

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

/*
 * Problem 731: A Stoneham Number
 *
 * A = sum_{i=1}^{inf} 1/(3^i * 10^{3^i})
 * A(n) = 10 digits of A starting at decimal position n.
 *
 * For each term i with 3^i <= n+9, compute digits of 1/3^i at offset (n - 3^i)
 * using modular exponentiation: 10^offset mod 3^i.
 */

typedef unsigned long long ull;
typedef __int128 lll;

ull power_mod(ull base, ull exp, ull mod) {
    ull result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = (lll)result * base % mod;
        base = (lll)base * base % mod;
        exp >>= 1;
    }
    return result;
}

// Get num_digits of 1/3^i starting at position 'start'
vector<int> digits_inv3i(int i, ull start, int num_digits) {
    // 3^i as big number - for i up to ~34, this fits in 128 bits
    // Use Python for the actual large computation; here demonstrate for small i
    ull mod = 1;
    for (int j = 0; j < i; j++) mod *= 3;

    ull r = power_mod(10, start, mod);
    vector<int> result;
    for (int k = 0; k < num_digits; k++) {
        ull nr = (lll)10 * r;
        result.push_back(nr / mod);
        r = nr % mod;
    }
    return result;
}

int main() {
    // Verify A(100)
    // Terms: i=1 (3^1=3), i=2 (9), i=3 (27), i=4 (81)
    int num_digits = 10;
    int extra = 20;
    ull n = 100;

    // For small cases, accumulate
    // (Full implementation needs arbitrary precision for large i)
    printf("A(10^16) requires Python/mpz for 3^34 modular arithmetic\n");

    return 0;
}