All Euler problems
Project Euler

Digital Root Sums of Factorisations

The digital root of a number is found by repeatedly summing its digits until a single digit remains. For example, dr(627) = dr(15) = 6. For a composite number n, a "digital root sum" (drs) of a fac...

Source sync Apr 19, 2026
Problem #0159
Level Level 07
Solved By 3,846
Languages C++, Python
Answer 14489159
Length 301 words
algebramodular_arithmeticbrute_force

Problem Statement

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

A composite number can be factored many different ways.

For instance, not including multiplication by one, $24$ can be factored in $7$ distinct ways: \begin{align*} 24 &= 2 \times 2 \times 2 \times 3\\ 24 &= 2 \times 3 \times 4\\ 24 &= 2 \times 2 \times 6\\ 24 &= 4 \times 6\\ 24 &= 3 \times 8\\ 24 &= 2 \times 12\\ 24 &= 24 \end{align*} Recall that the digital root of a number, in base $10$, is found by adding together the digits of that number, and repeating that process until a number is arrived at that is less than $10$. Thus the digital root of $467$ is $8$.

We shall call a Digital Root Sum (DRS) the sum of the digital roots of the individual factors of our number.

The chart below demonstrates all of the DRS values for $24$.

FactorisationDigital Root Sum
$2 \times 2 \times 2 \times 2$$9$
$2 \times 3 \times 4$$9$
$2 \times 2 \times 6$$10$
$4 \times 6$$10$
$3 \times 8$$11$
$2 \times 12$$5$
$24$$6$

The maximum Digital Root Sum of $24$ is $11$.

The function $\operatorname{mdrs}(n)$ gives the maximum Digital Root Sum of $n$. So $\operatorname{mdrs}(24)=11$.

Find $\displaystyle \sum \operatorname{mdrs}(n)$ for $1 < n < 1\,000\,000$.

Problem 159: Digital Root Sums of Factorisations

Mathematical Analysis

Digital root formula

The digital root has a well-known closed form:

dr(n)=1+((n1)mod9)dr(n) = 1 + ((n - 1) \bmod 9)

for n1n \ge 1. Equivalently, dr(n)=nmod9dr(n) = n \bmod 9 if n≢0(mod9)n \not\equiv 0 \pmod{9}, and dr(n)=9dr(n) = 9 if n0(mod9)n \equiv 0 \pmod{9} (for n>0n > 0).

Key property

For the digital root sum, note that dr(a)+dr(b)dr(ab)dr(a) + dr(b) \ge dr(ab) in general. This means splitting a factor into smaller factors can only maintain or increase the digital root sum.

Dynamic programming approach

We compute mdrs(n)\text{mdrs}(n) using a sieve-like approach:

Initialize mdrs(n)=dr(n)\text{mdrs}(n) = dr(n) for all nn (the trivial factorization with nn itself).

Then for each aa from 2 to 999999\sqrt{999999}, for each multiple n=abn = a \cdot b where bab \ge a:

mdrs(n)=max(mdrs(n),  dr(a)+mdrs(b))\text{mdrs}(n) = \max(\text{mdrs}(n),\; dr(a) + \text{mdrs}(b))

This works because the optimal factorization of nn either has nn as a single factor (giving dr(n)dr(n)), or splits as n=abn = a \cdot b for some a2a \ge 2, and we use the optimal factorization of bb.

We iterate aa from 2 upward, and for each aa, update all multiples. Since we process aa in increasing order and use the already-computed mdrs(b)\text{mdrs}(b) values, this correctly computes the maximum.

Correctness

If the optimal factorization of nn is a1a2aka_1 \le a_2 \le \cdots \le a_k, then:

  • The smallest factor is a12a_1 \ge 2.
  • The remaining product is b=n/a1b = n/a_1 with ba1b \ge a_1.
  • mdrs(n)=dr(a1)+mdrs(b)\text{mdrs}(n) = dr(a_1) + \text{mdrs}(b).

Since we iterate aa from 2 up and update mdrs(n)\text{mdrs}(n) to the maximum of its current value and dr(a)+mdrs(n/a)dr(a) + \text{mdrs}(n/a), we capture this optimal split.

Correctness

Theorem. The method described above computes exactly the quantity requested in the problem statement.

Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer. \square

Complexity

The sieve runs in O(NlogN)O(N \log N) time where N=999999N = 999999, since for each aa, we process O(N/a)O(N/a) multiples.

Answer

14489159\boxed{14489159}

Code

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

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

const int LIMIT = 1000000;

int dr(int n) {
    if (n == 0) return 0;
    int r = n % 9;
    return r == 0 ? 9 : r;
}

int mdrs[LIMIT];

int main() {
    ios::sync_with_stdio(false);

    // Initialize mdrs[n] = dr(n)
    for (int n = 2; n < LIMIT; n++) {
        mdrs[n] = dr(n);
    }

    // Sieve: for each factor a >= 2, update multiples
    for (int a = 2; a * a < LIMIT; a++) {
        for (int j = a; (long long)a * j < LIMIT; j++) {
            int n = a * j;
            mdrs[n] = max(mdrs[n], dr(a) + mdrs[j]);
        }
    }

    long long total = 0;
    for (int n = 2; n < LIMIT; n++) {
        total += mdrs[n];
    }

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