This operation turns the set of arithmetic functions into a commutative ring, and most classical number-theoretic identities are one-line statements in it.

The algebra

  • Identity: , so .
  • Commutative and associative.
  • The convolution of two multiplicative functions is multiplicative.
  • Every with has an inverse under .

The standard functions

SymbolDefinition
— the identity
for all
Möbius
Euler totient
number of divisors
sum of divisors

The identities — all one line each

Möbius inversion is just: , which follows immediately from and associativity.

Seeing these as ring identities makes them memorable — you derive them instead of memorising them.

Computing a convolution

All values up to :

vector<long long> conv(N + 1, 0);
for (int i = 1; i <= N; i++)
    for (int j = i; j <= N; j += i)
        conv[j] += f[i] * g[j / i];

The harmonic-series sieve again.

Du’s sieve — prefix sums for huge

To compute for up to , find a such that and both have easy prefix sums. Then

so isolating the term gives the recursion

Evaluate with divisor blocks ( distinct values of ), memoise the results, and precompute for with a linear sieve.

Complexity: with the sieve cutoff at .

TargetChoose Because
(Mertens), whose prefix sum is 1
, prefix sum
map<long long,long long> memo;
long long S(long long n) {                        // sum of phi(1..n)
    if (n <= CUT) return pre[n];
    auto it = memo.find(n);
    if (it != memo.end()) return it->second;
    long long res = n % 2 ? (n+1)/2 % MOD * (n % MOD) : n/2 % MOD * ((n+1) % MOD);
    for (long long l = 2, r; l <= n; l = r + 1) {
        r = n / (n / l);
        res -= (r - l + 1) * S(n / l);
    }
    return memo[n] = res;
}
  • Min_25 sieve and Powerful Number sieve — compute for multiplicative that is a polynomial on primes, in or better. More general than Du’s sieve, considerably more intricate.
  • Dirichlet hyperbola method in when both prefix sums are known:

Why bother

Problems asking for or the number of coprime pairs below are unreachable by sieving. Du’s sieve makes them routine, and the whole framework rests on recognising the right Dirichlet identity.

See also: Möbius Function · Euler Totient · Divisor Functions