All Euler problems
Project Euler

Arithmetic Derivative

The arithmetic derivative is defined by: p' = 1 for any prime p (ab)' = a'b + ab' (Leibniz product rule) 0' = 0, 1' = 0 Define ad(n) = n' as the arithmetic derivative of n. Compute sums of ad(n) ov...

Source sync Apr 19, 2026
Problem #0484
Level Level 23
Solved By 493
Languages C++, Python
Answer 8907904768686152599
Length 308 words
number_theoryanalytic_mathmodular_arithmetic

Problem Statement

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

The arithmetic derivative is defined by

  • \(p^\prime = 1\) for any prime \(p\)

  • \((ab)^\prime = a^\prime b + ab^\prime \) for all integers \(a, b\) (Leibniz rule)

For example, \(20^\prime = 24\).

Find \(\displaystyle {\sum } \operatorname {\mathbf {gcd}}(k,k^\prime )\) for \(1 < k \le 5 \times 10^{15}\).

Note: \(\operatorname {\mathbf {gcd}}(x,y)\) denotes the greatest common divisor of \(x\) and \(y\).

Problem 484: Arithmetic Derivative

Mathematical Foundation

Theorem 1 (Closed form for the arithmetic derivative). For any positive integer n2n \ge 2 with prime factorization n=p1a1p2a2pkakn = p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k},

n=ni=1kaipi.n' = n \sum_{i=1}^{k} \frac{a_i}{p_i}.

Proof. By strong induction on Ω(n)=iai\Omega(n) = \sum_i a_i (the number of prime factors counted with multiplicity).

Base case: Ω(n)=1\Omega(n) = 1 means n=pn = p is prime. Then n=1=p(1/p)n' = 1 = p \cdot (1/p), which matches the formula with k=1k = 1, a1=1a_1 = 1.

Inductive step: Suppose the formula holds for all integers with fewer than Ω(n)\Omega(n) prime factors. Write n=pmn = p \cdot m where pp is a prime dividing nn. By the Leibniz rule,

n=pm+pm=m+pm.n' = p' \cdot m + p \cdot m' = m + p \cdot m'.

Case 1: gcd(p,m)=1\gcd(p, m) = 1. Then m=i=2kpiaim = \prod_{i=2}^k p_i^{a_i} and Ω(m)<Ω(n)\Omega(m) < \Omega(n). By the inductive hypothesis, m=mi=2kai/pim' = m \sum_{i=2}^k a_i/p_i. Thus

n=m+pmi=2kaipi=m ⁣(1+pi=2kaipi)=np ⁣(1+pi=2kaipi)=n ⁣(1p+i=2kaipi),n' = m + pm \sum_{i=2}^k \frac{a_i}{p_i} = m\!\left(1 + p\sum_{i=2}^k \frac{a_i}{p_i}\right) = \frac{n}{p}\!\left(1 + p\sum_{i=2}^k \frac{a_i}{p_i}\right) = n\!\left(\frac{1}{p} + \sum_{i=2}^k \frac{a_i}{p_i}\right),

which matches the formula since a1=1a_1 = 1 for prime p=p1p = p_1.

Case 2: pmp \mid m. Then m=pa11i=2kpiaim = p^{a_1 - 1} \prod_{i=2}^k p_i^{a_i} and Ω(m)=Ω(n)1<Ω(n)\Omega(m) = \Omega(n) - 1 < \Omega(n). By the inductive hypothesis,

m=m ⁣(a11p+i=2kaipi).m' = m\!\left(\frac{a_1 - 1}{p} + \sum_{i=2}^k \frac{a_i}{p_i}\right).

Therefore

n=m+pm ⁣(a11p+i=2kaipi)=m ⁣(1+(a11)+pi=2kaipi)=m ⁣(a1+pi=2kaipi).n' = m + pm\!\left(\frac{a_1-1}{p} + \sum_{i=2}^k \frac{a_i}{p_i}\right) = m\!\left(1 + (a_1-1) + p\sum_{i=2}^k \frac{a_i}{p_i}\right) = m\!\left(a_1 + p\sum_{i=2}^k \frac{a_i}{p_i}\right).

Since m=n/pm = n/p:

n=np ⁣(a1+pi=2kaipi)=n ⁣(a1p+i=2kaipi),n' = \frac{n}{p}\!\left(a_1 + p\sum_{i=2}^k \frac{a_i}{p_i}\right) = n\!\left(\frac{a_1}{p} + \sum_{i=2}^k \frac{a_i}{p_i}\right),

which is the claimed formula. \square

Lemma 1 (Equivalent additive form). For n=p1a1pkakn = p_1^{a_1} \cdots p_k^{a_k},

n=i=1kainpi.n' = \sum_{i=1}^k a_i \cdot \frac{n}{p_i}.

Proof. Multiply the formula in Theorem 1 by n/n=1n/n = 1: n=niai/pi=iai(n/pi)n' = n \sum_i a_i/p_i = \sum_i a_i \cdot (n/p_i). \square

Theorem 2 (Summation interchange). For the sum n=2Nn\sum_{n=2}^{N} n', we have

n=2Nn=pN1pnNpnnvp(n)\sum_{n=2}^{N} n' = \sum_{p \le N} \frac{1}{p} \sum_{\substack{n \le N \\ p \mid n}} n \cdot v_p(n)

where the outer sum is over primes pNp \le N and vp(n)v_p(n) is the pp-adic valuation of nn.

Proof. By Theorem 1, n=pnvp(n)n/pn' = \sum_{p \mid n} v_p(n) \cdot n/p. Summing over nn and exchanging the order of summation (justified since all terms are non-negative):

n=2Nn=n=2Npnvp(n)np=pN1p2nNpnnvp(n).\sum_{n=2}^N n' = \sum_{n=2}^N \sum_{p \mid n} \frac{v_p(n) \cdot n}{p} = \sum_{p \le N} \frac{1}{p} \sum_{\substack{2 \le n \le N \\ p \mid n}} n \cdot v_p(n). \quad \square

Lemma 2 (Inner sum decomposition). For a fixed prime pp,

nNpnnvp(n)=a=1logpNapamN/pagcd(m,p)=1m.\sum_{\substack{n \le N \\ p \mid n}} n \cdot v_p(n) = \sum_{a=1}^{\lfloor \log_p N \rfloor} a \cdot p^a \sum_{\substack{m \le N/p^a \\ \gcd(m, p) = 1}} m.

Proof. Write n=pamn = p^a m where a=vp(n)1a = v_p(n) \ge 1 and gcd(m,p)=1\gcd(m, p) = 1. Then nvp(n)=paman \cdot v_p(n) = p^a m \cdot a. Grouping by aa:

nNpnnvp(n)=a1mN/pagcd(m,p)=1apam.\sum_{\substack{n \le N \\ p \mid n}} n \cdot v_p(n) = \sum_{a \ge 1} \sum_{\substack{m \le N/p^a \\ \gcd(m,p)=1}} a \cdot p^a \cdot m. \quad \square

The inner sum mMgcd(m,p)=1m\sum_{\substack{m \le M \\ \gcd(m,p)=1}} m can be computed as m=1Mmpm=1M/pm\sum_{m=1}^M m - p \sum_{m=1}^{\lfloor M/p \rfloor} m using inclusion-exclusion.

Editorial

The arithmetic derivative n’ is defined by: p’ = 1 for prime p, (ab)’ = a’b + ab’ (product rule), 0’ = 1’ = 0. For n = p1^a1 * … * pk^ak, n’ = n * sum(ai/pi). We method 1: Sieve-based (for moderate N). We then method 2: Prime-sum approach (for large N). Finally, sum of m in [1, M] with gcd(m, p) = 1.

Pseudocode

Method 1: Sieve-based (for moderate N)
Method 2: Prime-sum approach (for large N)
sum of m in [1, M] with gcd(m, p) = 1

Complexity Analysis

  • Time (Sieve method): O(NloglogN)O(N \log \log N) for the smallest-prime-factor sieve, plus O(NlogN)O(N \log N) for computing all derivatives (each nn has O(logn)O(\log n) prime factors). Total: O(NlogN)O(N \log N).
  • Space (Sieve method): O(N)O(N) for the sieve array.
  • Time (Prime-sum method): O(π(N)logN)=O(N/lnNlogN)=O(N)O(\pi(N) \cdot \log N) = O(N / \ln N \cdot \log N) = O(N).
  • Space (Prime-sum method): O(N)O(\sqrt{N}) for the prime sieve (segmented).

Answer

8907904768686152599\boxed{8907904768686152599}

Code

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

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

// Compute arithmetic derivative of n
// For n = p1^a1 * ... * pk^ak, n' = n * sum(ai / pi) = sum(ai * n / pi)
long long arithmetic_derivative(long long n) {
    if (n <= 1) return 0;
    long long result = 0;
    long long temp = n;
    for (long long d = 2; d * d <= temp; d++) {
        while (temp % d == 0) {
            result += n / d;
            temp /= d;
        }
    }
    if (temp > 1) result += n / temp;
    return result;
}

// Sieve-based computation of sum of arithmetic derivatives
long long solve_sieve(int limit) {
    // Smallest prime factor sieve
    vector<int> spf(limit + 1);
    iota(spf.begin(), spf.end(), 0);
    for (int i = 2; (long long)i * i <= limit; i++) {
        if (spf[i] == i) {
            for (int j = i * i; j <= limit; j += i) {
                if (spf[j] == j) spf[j] = i;
            }
        }
    }

    long long total = 0;
    for (int n = 2; n <= limit; n++) {
        int temp = n;
        long long ad = 0;
        while (temp > 1) {
            int p = spf[temp];
            int a = 0;
            while (temp % p == 0) {
                temp /= p;
                a++;
            }
            ad += (long long)a * (n / p);
        }
        total += ad;
    }
    return total;
}

int main() {
    // Demonstration: sum of arithmetic derivatives for n = 2..1000
    long long ans = solve_sieve(1000);
    cout << "Sum of ad(n) for n=2..1000: " << ans << endl;

    // Verify with direct computation
    long long check = 0;
    for (int n = 2; n <= 1000; n++) {
        check += arithmetic_derivative(n);
    }
    assert(ans == check);

    // For the full problem, scale up as needed
    // The sieve approach handles up to ~10^8 with enough memory
    return 0;
}