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 the formulas are
A function is multiplicative when for coprime ; 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 is the exponent of the unit group: for , otherwise, and the lcm across the prime powers. It divides , 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 is perfect when is prime; Euler showed every even perfect number has that form. perfectNumbers finds the Mersenne primes with the Lucas–Lehmer test: with , , is prime exactly when . Whether an odd perfect number exists is open. The aliquot sequence iterates ; 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 . It is commutative and associative, with identity (1 at 1, 0 elsewhere), and it turns the classical identities into one-liners:
Möbius inversion follows from the first: if , then . So is recovered from , and from .
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
@ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.