Arithmetic functions

Arithmetic functions

Functions on the positive integers that see a number through its divisors: how many there are, what they add to, how many residues are units. Most are multiplicative, so they come from the factorisation, and Dirichlet convolution ties them together.

The usual suspects

import { divisors, sigma, tau, totient, mobius, carmichael } from "@ratmath/numtheory";

divisors(60);     // [1n, 2n, 3n, 4n, 5n, 6n, 10n, 12n, 15n, 20n, 30n, 60n]
tau(60);          // 12n   the number of divisors
sigma(60);        // 168n  their sum;  sigma(60, 2) = 5460n, the sum of their squares
totient(60);      // 16n   residues prime to 60
carmichael(60);   // 4n    every unit mod 60 has a⁴ ≡ 1
mobius(60);       // 0n    4 divides 60;  mobius(30) = -1n
omega(60);        // 3n distinct primes;  bigOmega(60) = 4n with multiplicity

For n=∏pen = \prod p^{e} the formulas are

τ(n)=∏(e+1),σ(n)=∏pe+1−1p−1,φ(n)=n∏p∣n(1−1p). \tau(n) = \prod (e + 1), \qquad \sigma(n) = \prod \frac{p^{e+1} - 1}{p - 1}, \qquad \varphi(n) = n \prod_{p \mid n}\Bigl(1 - \frac1p\Bigr).

A function is multiplicative when f(mn)=f(m)f(n)f(mn) = f(m)f(n) for coprime m,nm, n; all of these are, so each is fixed by its values at prime powers. isMultiplicative(f) tests that on examples (it is a check, not a proof). Carmichael's λ(n)\lambda(n) is the exponent of the unit group: λ(2e)=2e−2\lambda(2^e) = 2^{e-2} for e≥3e \ge 3, λ(pe)=φ(pe)\lambda(p^e) = \varphi(p^e) otherwise, and the lcm across the prime powers. It divides φ(n)\varphi(n), and it is smaller exactly when there is no primitive root.

Perfect, abundant, amicable

classify(28);               // "perfect": σ(28) = 56 = 2·28
perfectNumbers(6);          // [6n, 28n, 496n, 8128n, 33550336n, 8589869056n]
amicablePairs(10000);       // [[220n, 284n], [1184n, 1210n], [2620n, 2924n], [5020n, 5564n], [6232n, 6368n]]
aliquotSequence(12);        // 12, 16, 15, 9, 4, 3, 1, 0: "terminates"
aliquotSequence(12496).ending;   // "cycle of length 5": a sociable chain
aliquotSequence(276, { steps: 30 }).ending;   // "unfinished": nobody knows how 276 ends

Euclid showed 2p−1(2p−1)2^{p-1}(2^p - 1) is perfect when 2p−12^p - 1 is prime; Euler showed every even perfect number has that form. perfectNumbers finds the Mersenne primes with the Lucas–Lehmer test: with s0=4s_0 = 4, sk+1=sk2−2s_{k+1} = s_k^2 - 2, 2p−12^p - 1 is prime exactly when sp−2≡0s_{p-2} \equiv 0. Whether an odd perfect number exists is open. The aliquot sequence iterates s(n)=σ(n)−ns(n) = \sigma(n) - n; Catalan and Dickson conjectured that it always ends or cycles, and 276 is the smallest start nobody has followed to the end.

Dirichlet convolution

The convolution of two arithmetic functions is (f∗g)(n)=∑d∣nf(d) g(n/d)(f * g)(n) = \sum_{d \mid n} f(d)\,g(n/d). It is commutative and associative, with identity ε\varepsilon (1 at 1, 0 elsewhere), and it turns the classical identities into one-liners:

μ∗1=ε,φ∗1=id,id∗1=σ. \mu * 1 = \varepsilon, \qquad \varphi * 1 = \mathrm{id}, \qquad \mathrm{id} * 1 = \sigma.

Möbius inversion follows from the first: if F=f∗1F = f * 1, then f=F∗μf = F * \mu. So φ\varphi is recovered from ∑d∣nφ(d)=n\sum_{d\mid n}\varphi(d) = n, and id\mathrm{id} from σ\sigma.

const e = dirichlet(mobius, one);
[1n, 2n, 3n, 4n, 5n, 6n].map(e);              // [1n, 0n, 0n, 0n, 0n, 0n]: ε
[1n, 2n, 6n, 12n].map(dirichlet(totient, one)); // [1n, 2n, 6n, 12n]: id
[1n, 2n, 3n, 4n].map(mobiusInversion(sigma));  // [1n, 2n, 3n, 4n]: σ inverted is id
Try itEdit the code and run it with @ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs