All Euler problems
Project Euler

Sum of Radical Expressions

For a positive integer n, define R(n) = sum_(d | n) rad(d), where rad(d) is the product of the distinct prime factors of d (with rad(1) = 1). Find sum_(n=1)^(10^7) R(n) mod (10^9 + 7).

Source sync May 21, 2026
Problem #0931
Level Level 37
Solved By 169
Languages C++, Python
Answer 128856311
Length 232 words
modular_arithmeticnumber_theorybrute_force

Problem Statement

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

For a positive integer construct a graph using all the divisors of as the vertices. An edge is drawn between and if is divisible by and is prime, and is given weight , where is the Euler totient function.
Define to be the total weight of this graph.
The example below shows that

0931_totientgraph.png

Let . You are given and .

Find . Give your answer modulo .

Problem 931: Sum of Radical Expressions

Mathematical Foundation

Definition. For n≥1n \geq 1, the radical of nn is rad⁡(n)=∏p∣np\operatorname{rad}(n) = \prod_{p \mid n} p, with rad⁡(1)=1\operatorname{rad}(1) = 1.

Lemma 1. The radical function rad⁡\operatorname{rad} is multiplicative: if gcd⁡(a,b)=1\gcd(a, b) = 1, then rad⁡(ab)=rad⁡(a)⋅rad⁡(b)\operatorname{rad}(ab) = \operatorname{rad}(a) \cdot \operatorname{rad}(b).

Proof. Let a=∏piaia = \prod p_i^{a_i} and b=∏qjbjb = \prod q_j^{b_j} where {pi}\{p_i\} and {qj}\{q_j\} are disjoint (since gcd⁡(a,b)=1\gcd(a,b) = 1). Then ab=∏piai∏qjbjab = \prod p_i^{a_i} \prod q_j^{b_j} and the set of distinct prime factors of abab is {pi}∪{qj}\{p_i\} \cup \{q_j\}. Hence rad⁡(ab)=∏pi⋅∏qj=rad⁡(a)⋅rad⁡(b)\operatorname{rad}(ab) = \prod p_i \cdot \prod q_j = \operatorname{rad}(a) \cdot \operatorname{rad}(b). □\square

Theorem 1. The function R(n)=∑d∣nrad⁡(d)R(n) = \sum_{d \mid n} \operatorname{rad}(d) is multiplicative, and for a prime power pap^a with a≥1a \geq 1:

R(pa)=1+ap.R(p^a) = 1 + ap.

Proof. Since R=rad⁡∗1R = \operatorname{rad} * \mathbf{1} is the Dirichlet convolution of two multiplicative functions, RR is multiplicative (a standard result in multiplicative number theory). For the prime power formula, the divisors of pap^a are 1,p,p2,…,pa1, p, p^2, \ldots, p^a. We have rad⁡(1)=1\operatorname{rad}(1) = 1 and rad⁡(pk)=p\operatorname{rad}(p^k) = p for all k≥1k \geq 1. Therefore:

R(pa)=∑k=0arad⁡(pk)=1+∑k=1ap=1+ap.R(p^a) = \sum_{k=0}^{a} \operatorname{rad}(p^k) = 1 + \sum_{k=1}^{a} p = 1 + ap.

□\square

Theorem 2 (Closed-form product). For n=p1a1⋯prarn = p_1^{a_1} \cdots p_r^{a_r}:

R(n)=∏i=1r(1+ai pi).R(n) = \prod_{i=1}^{r} (1 + a_i \, p_i).

Proof. Immediate from Theorem 1 and the multiplicativity of RR. □\square

Theorem 3 (Summation by Dirichlet interchange). For any N≥1N \geq 1:

∑n=1NR(n)=∑d=1Nrad⁡(d)⋅⌊Nd⌋.\sum_{n=1}^{N} R(n) = \sum_{d=1}^{N} \operatorname{rad}(d) \cdot \left\lfloor \frac{N}{d} \right\rfloor.

Proof. We have:

∑n=1NR(n)=∑n=1N∑d∣nrad⁡(d)=∑d=1Nrad⁡(d)∑n=1d∣nN1=∑d=1Nrad⁡(d)⋅⌊Nd⌋,\sum_{n=1}^{N} R(n) = \sum_{n=1}^{N} \sum_{d \mid n} \operatorname{rad}(d) = \sum_{d=1}^{N} \operatorname{rad}(d) \sum_{\substack{n=1 \\ d \mid n}}^{N} 1 = \sum_{d=1}^{N} \operatorname{rad}(d) \cdot \left\lfloor \frac{N}{d} \right\rfloor,

where the second equality follows by swapping the order of summation (each pair (d,n)(d, n) with d∣nd \mid n and 1≤n≤N1 \leq n \leq N is counted once on each side). □\square

Editorial

Alternative (direct product approach using Theorem 2):*. We linear sieve to compute rad(d) for all d <= N. We then iterate over p from 2 to N. Finally, compute the sum via Dirichlet interchange (Theorem 3).

Pseudocode

Linear sieve to compute rad(d) for all d <= N
for p from 2 to N
Compute the sum via Dirichlet interchange (Theorem 3)
for d from 1 to N
Sieve smallest prime factor spf[n] for n <= N
for p from 2 to N
For each n, compute R(n) = prod(1 + a_i * p_i)
for n from 1 to N

Complexity Analysis

  • Time: O(Nlog⁡log⁡N)O(N \log \log N) for the sieve; O(N)O(N) for the summation in the Dirichlet interchange approach. The direct factorization approach takes O(Nlog⁡N)O(N \log N) in the worst case (due to repeated division), but O(N)O(N) amortized. Overall: O(Nlog⁡log⁡N)O(N \log \log N).
  • Space: O(N)O(N) for storing rad⁡(d)\operatorname{rad}(d) or the smallest prime factor array.

Answer

128856311\boxed{128856311}

Code

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

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

/*
 * Problem 931: Sum of Radical Expressions
 *
 * R(n) = sum_{d|n} rad(d). Find sum_{n=1}^{10^7} R(n) mod 10^9+7.
 *
 * rad(n) = product of distinct prime factors.
 * R is multiplicative: R(p^a) = 1 + a*p.
 * R(n) = prod_{p^a || n} (1 + a*p).
 *
 * Alternative: sum R(n) = sum_{d=1}^N rad(d) * floor(N/d).
 *
 * Algorithm: sieve to compute rad(d) for all d, then compute sum.
 */

int main() {
    const int N = 10000000;
    const long long MOD = 1000000007;
    
    // Compute rad(n) via sieve
    vector<long long> rad(N + 1, 1);
    for (int p = 2; p <= N; p++) {
        if (rad[p] == 1) { // p is prime
            for (int m = p; m <= N; m += p) {
                rad[m] *= p;
            }
        }
    }
    
    // sum R(n) = sum_{d=1}^N rad(d) * floor(N/d)
    long long total = 0;
    for (int d = 1; d <= N; d++) {
        total = (total + rad[d] % MOD * (N / d) % MOD) % MOD;
    }
    cout << total << endl;

    // Verify: R(6) = rad(1)+rad(2)+rad(3)+rad(6) = 1+2+3+6 = 12
    assert(rad[1] == 1 && rad[2] == 2 && rad[3] == 3 && rad[6] == 6);

    return 0;
}