All Euler problems
Project Euler

Champernowne's Constant

An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... If d_n represents the n -th digit of the fractional part, find the value of:...

Source sync Apr 19, 2026
Problem #0040
Level Level 01
Solved By 88,115
Languages C++, Python
Answer 210
Length 444 words
modular_arithmeticbrute_forcesequence

Problem Statement

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

An irrational decimal fraction is created by concatenating the positive integers: $$0.12345678910{\color{red}\mathbf 1}112131415161718192021\cdots$$ It can be seen that the $12^{th}$ digit of the fractional part is $1$.

If $d_n$ represents the $n^{th}$ digit of the fractional part, find the value of the following expression. $$d_1 \times d_{10} \times d_{100} \times d_{1000} \times d_{10000} \times d_{100000} \times d_{1000000}$$

Problem 40: Champernowne’s Constant

Mathematical Development

Mathematical Foundation

Definition. Champernowne’s constant (base 10) is C10=0.123456789101112C_{10} = 0.123456789101112\ldots, formed by concatenating all positive integers in order. We denote by dnd_n the nn-th digit of the fractional part.

Theorem 1 (Block structure). The digits of Champernowne’s constant are partitioned into blocks by digit count kk. The kk-digit positive integers range from 10k110^{k-1} to 10k110^k - 1, contributing a total of 9×10k1×k9 \times 10^{k-1} \times k digits.

Proof. The number of kk-digit positive integers is 10k10k1=9×10k110^k - 10^{k-1} = 9 \times 10^{k-1}. Each contributes kk digits to the concatenation, giving 9×10k1×k9 \times 10^{k-1} \times k digits total from the kk-digit block. \square

Lemma 1 (Cumulative digit positions). Let Lk=j=1k9×10j1×jL_k = \sum_{j=1}^{k} 9 \times 10^{j-1} \times j be the cumulative number of digits contributed by all integers with at most kk digits. Then:

kk9×10k1×k9 \times 10^{k-1} \times kLkL_k
199
2180189
327002889
43600038889
5450000488889
654000005888889

Proof. Direct computation: L1=9L_1 = 9, L2=9+180=189L_2 = 9 + 180 = 189, L3=189+2700=2889L_3 = 189 + 2700 = 2889, etc. \square

Theorem 2 (Digit extraction algorithm). To find dnd_n:

  1. Find the unique kk such that Lk1<nLkL_{k-1} < n \le L_k (with L0=0L_0 = 0).
  2. Compute the offset r=nLk1r = n - L_{k-1} within the kk-digit block.
  3. The integer containing the nn-th digit is q=10k1+(r1)/kq = 10^{k-1} + \lfloor (r - 1) / k \rfloor.
  4. The position within qq is j=(r1)modkj = (r - 1) \bmod k (0-indexed from the left).
  5. Then dnd_n is the jj-th digit (from the left) of qq.

Proof. After consuming Lk1L_{k-1} digits from all integers with fewer than kk digits, position nn falls within the kk-digit block. The offset rr counts how many digits into this block position nn lies. Since each kk-digit integer contributes exactly kk digits, the (r1)/k\lfloor (r-1)/k \rfloor-th integer in the block (0-indexed) is 10k1+(r1)/k10^{k-1} + \lfloor (r-1)/k \rfloor. Within this integer, the digit position is (r1)modk(r-1) \bmod k from the left. \square

Theorem 3 (Computation of required digits). Applying Theorem 2 to each required position:

  • d1d_1: k=1k=1, r=1r = 1. Integer =1+0/1=1= 1 + \lfloor 0/1 \rfloor = 1. Digit index 00 of “1” d1=1\Rightarrow d_1 = 1.

  • d10d_{10}: k=2k=2, r=109=1r = 10 - 9 = 1. Integer =10+0/2=10= 10 + \lfloor 0/2 \rfloor = 10. Digit index 00 of “10” d10=1\Rightarrow d_{10} = 1.

  • d100d_{100}: k=2k=2, r=1009=91r = 100 - 9 = 91. Integer =10+90/2=10+45=55= 10 + \lfloor 90/2 \rfloor = 10 + 45 = 55. Digit index 90mod2=090 \bmod 2 = 0 of “55” d100=5\Rightarrow d_{100} = 5.

  • d1000d_{1000}: k=3k=3, r=1000189=811r = 1000 - 189 = 811. Integer =100+810/3=100+270=370= 100 + \lfloor 810/3 \rfloor = 100 + 270 = 370. Digit index 810mod3=0810 \bmod 3 = 0 of “370” d1000=3\Rightarrow d_{1000} = 3.

  • d10000d_{10000}: k=4k=4, r=100002889=7111r = 10000 - 2889 = 7111. Integer =1000+7110/4=1000+1777=2777= 1000 + \lfloor 7110/4 \rfloor = 1000 + 1777 = 2777. Digit index 7110mod4=27110 \bmod 4 = 2 of “2777” d10000=7\Rightarrow d_{10000} = 7.

  • d100000d_{100000}: k=5k=5, r=10000038889=61111r = 100000 - 38889 = 61111. Integer =10000+61110/5=10000+12222=22222= 10000 + \lfloor 61110/5 \rfloor = 10000 + 12222 = 22222. Digit index 61110mod5=061110 \bmod 5 = 0 of “22222” d100000=2\Rightarrow d_{100000} = 2.

  • d1000000d_{1000000}: k=6k=6, r=1000000488889=511111r = 1000000 - 488889 = 511111. Integer =100000+511110/6=100000+85185=185185= 100000 + \lfloor 511110/6 \rfloor = 100000 + 85185 = 185185. Digit index 511110mod6=0511110 \bmod 6 = 0 of “185185” d1000000=1\Rightarrow d_{1000000} = 1.

Proof. Each computation follows directly from Theorem 2 with the values from Lemma 1. \square

Editorial

We use the block decomposition of Champernowne’s constant by digit length. For any requested position nn, the procedure first determines which block of kk-digit integers contains that position by removing the contributions of all shorter blocks. The remaining offset identifies the exact integer in the kk-digit block and the exact digit inside that integer. Repeating this extraction for the seven positions 100,101,,10610^0,10^1,\ldots,10^6 and multiplying the resulting digits yields the required product.

Pseudocode

Algorithm: Digit of Champernowne Constant
Require: An integer n ≥ 1.
Ensure: The digit d_n in the fractional part of Champernowne's constant.
1: Initialize k ← 1, block ← 9, and offset ← n.
2: While offset > k · block do:
3:     Update offset ← offset - k · block, k ← k + 1, and block ← 9 · 10^(k - 1).
4: Set q ← 10^(k - 1) + floor((offset - 1) / k) and j ← (offset - 1) mod k.
5: Return the (j + 1)-st digit of q from the left.
Algorithm: Champernowne Digit Product
Require: The target positions {10^0, 10^1, ..., 10^6}.
Ensure: The product ∏_{i=0}^6 d_(10^i).
1: Initialize P ← 1.
2: For each i in {0, 1, ..., 6} do:
3:     Compute d ← DigitOfChampernowneConstant(10^i) and update P ← P · d.
4: Return P.

Complexity Analysis

  • Time: O(log10n)O(\log_{10} n) per digit query, since we iterate through at most log10n+1\lfloor \log_{10} n \rfloor + 1 blocks. With 7 queries and n106n \le 10^6, total time is O(7×6)=O(1)O(7 \times 6) = O(1).
  • Space: O(1)O(1). Only a constant number of variables are needed.

Answer

210\boxed{210}

Code

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

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

int champernowne_digit(int n) {
    int k = 1;
    long long count = 9;
    long long start = 1;

    while (n > k * count) {
        n -= k * count;
        k++;
        count *= 10;
        start *= 10;
    }

    // n is now the position within the k-digit block (1-indexed)
    long long number = start + (n - 1) / k;
    int digit_pos = (n - 1) % k;

    string s = to_string(number);
    return s[digit_pos] - '0';
}

int main() {
    long long product = 1;
    for (int exp = 0; exp <= 6; exp++) {
        int pos = 1;
        for (int i = 0; i < exp; i++) pos *= 10;
        int d = champernowne_digit(pos);
        product *= d;
    }

    cout << product << endl;
    return 0;
}