All Euler problems
Project Euler

Digital Root Patterns

Let d(n) denote the digital root of n (the single digit obtained by repeatedly summing digits). Define S(N) = sum_(k=1)^N k * d(k). Find S(10^6) mod 10^9+7.

Source sync Apr 19, 2026
Problem #0901
Level Level 22
Solved By 554
Languages C++, Python
Answer 2.364497769
Length 160 words
modular_arithmeticalgebrabrute_force

Problem Statement

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

A driller drills for water. At each iteration the driller chooses a depth \(d\) (a positive real number), drills to this depth and then checks if water was found. If so, the process terminates. Otherwise, a new depth is chosen and a new drilling starts from the ground level in a new location nearby.

Drilling to depth \(d\) takes exactly \(d\) hours. The groundwater depth is constant in the relevant area and its distribution is known to be an exponential random variable with expected value of \(1\). In other words, the probability that the groundwater is deeper than \(d\) is \(e^{-d}\).

Assuming an optimal strategy, find the minimal expected drilling time in hours required to find water. Give your answer rounded to 9 places after the decimal point.

Problem 901: Digital Root Patterns

Mathematical Foundation

Theorem 1 (Digital Root Formula). For every positive integer nn,

d(n)=1+((n1)mod9).d(n) = 1 + \bigl((n-1) \bmod 9\bigr).

Proof. Write n=9q+rn = 9q + r where r{0,1,,8}r \in \{0, 1, \ldots, 8\}. If r1r \geq 1, then nr(mod9)n \equiv r \pmod{9} and d(n)=r=1+(n1)mod9d(n) = r = 1 + (n - 1) \bmod 9. If r=0r = 0, then n0(mod9)n \equiv 0 \pmod{9} and d(n)=9d(n) = 9, while 1+(n1)mod9=1+(9q1)mod9=1+8=91 + (n-1) \bmod 9 = 1 + (9q - 1) \bmod 9 = 1 + 8 = 9. In both cases the formula holds. \square

Lemma 1 (Block Sum). For each integer m0m \geq 0, the contribution of the block {9m+1,9m+2,,9m+9}\{9m+1, 9m+2, \ldots, 9m+9\} to SS is

B(m)=j=19(9m+j)j=405m+285.B(m) = \sum_{j=1}^{9}(9m+j)\cdot j = 405m + 285.

Proof. Expand using Theorem 1:

B(m)=9mj=19j+j=19j2=9m45+285=405m+285,B(m) = 9m\sum_{j=1}^{9} j + \sum_{j=1}^{9} j^2 = 9m \cdot 45 + 285 = 405m + 285,

where j=19j=45\sum_{j=1}^{9} j = 45 and j=19j2=285\sum_{j=1}^{9} j^2 = 285. \square

Theorem 2 (Closed-Form Evaluation). Let N=9q+rN = 9q + r with q=N/9q = \lfloor N/9 \rfloor and 0r<90 \leq r < 9. Then

S(N)=405q(q1)2+285q+j=1r(9q+j)j.S(N) = 405 \cdot \frac{q(q-1)}{2} + 285q + \sum_{j=1}^{r}(9q+j)\cdot j.

Proof. Partition {1,,N}\{1, \ldots, N\} into qq complete blocks of 9 and a remainder of rr terms. By Lemma 1, the complete blocks contribute

m=0q1B(m)=m=0q1(405m+285)=405q(q1)2+285q.\sum_{m=0}^{q-1} B(m) = \sum_{m=0}^{q-1}(405m + 285) = 405 \cdot \frac{q(q-1)}{2} + 285q.

The remainder block contributes j=1r(9q+j)j\sum_{j=1}^{r}(9q+j) \cdot j. Summing gives the result. \square

Corollary. For N=106N = 10^6: q=111111q = 111111, r=1r = 1, and the remainder contributes (9111111+1)1=1000000(9 \cdot 111111 + 1) \cdot 1 = 1000000. Hence

S(106)=4051111111111102+285111111+1000000=2499999166667.S(10^6) = 405 \cdot \frac{111111 \cdot 111110}{2} + 285 \cdot 111111 + 1000000 = 2499999166667.

Editorial

Let d(n) denote the digital root of n (the single digit obtained by repeatedly summing the digits of n). Define S(N) = sum_{k=1}^{N} k * d(k). Find S(10^6) mod 10^9 + 7.

Pseudocode

    q = N / 9 // integer division
    r = N mod 9
    total = 405 * q * (q - 1) / 2 + 285 * q
    For j from 1 to r:
        total += (9 * q + j) * j
    Return total mod (10^9 + 7)

Complexity Analysis

  • Time: O(1)O(1) (the remainder loop runs at most 8 iterations).
  • Space: O(1)O(1).

Answer

2.364497769\boxed{2.364497769}

Code

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

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

int main() {
    long long N = 1000000;
    long long MOD = 1000000007;
    long long q = N / 9, r = N % 9;
    long long total = (405 % MOD) * ((q % MOD) * (((q - 1) % MOD + MOD) % MOD) % MOD) % MOD;
    total = (total * 500000004) % MOD; // modular inverse of 2
    total = (total + 285 % MOD * (q % MOD)) % MOD;
    for (int j = 1; j <= r; j++) {
        total = (total + (9 * q + j) % MOD * j) % MOD;
    }
    cout << total << endl;
    return 0;
}