EulerPhi¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Examples¶
All examples below are verified against the current Mathilda build.
Implementation notes¶
builtin_eulerphi computes Euler's totient. It takes |n| (since phi(-n)=phi(n)), factors a working copy via the shared factorize_mpz cascade (trial division → Pollard rho → ECM), then applies phi(n) = n * prod (1 - 1/p_i) per distinct prime as phi <- (phi / p) * (p - 1) with GMP mpz_divexact/mpz_mul, keeping intermediates exact. phi(0) = 0, phi(1) = 1. Non-integer arguments return NULL. Its cost is dominated by the factorisation of n.
Listable,Protected.- Counts the number of positive integers less than or equal to $n$ that are relatively prime to $n$.
- Returns 0 for $n = 0$, and handles negative integers via $\phi(-n) = \phi(n)$.
- Accepts arbitrary-precision integers (
BigInt). Factorization runs in GMP
Attributes: Listable, Protected.
Implementation status¶
Stable — documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
References¶
- Source:
src/facint.c - Specification:
docs/spec/builtins/number-theory.md
Notes & additional examples¶
Worked examples¶
Notes¶
EulerPhi[n] counts the integers in 1..n coprime to n. The Mersenne
prime 2^61 - 1 is prime, so phi = p - 1. The last example is Gauss's
identity Sum phi(d) = n over the divisors d of 30, recovering 30
exactly.