eulerPhi

Euler's totient function: how many integers up to n are coprime to n.

Category: Mathematics > Number Theory

Syntax

p = eulerPhi(n)

Returns the number of integers k with 1 <= k <= n and gcd(k, n) = 1.

Description

Euler's totient phi(n) is computed from the prime factorization: if n = p1^a1 * ... * pk^ak, then phi(n) = n*(1 - 1/p1)*...*(1 - 1/pk). For a prime p the value is p - 1, and phi(1) = 1.

It gives the order of the group of units modulo n, which is why a^phi(n) = 1 (mod n) when gcd(a, n) = 1 (Euler's theorem). Non-integer input is an error.

Input arguments

n
Integer scalar or array, double or symbolic.

Output arguments

p
Array of the same size as n with the totient values.

Example

Totients of 1 to 6 and of 36

eulerPhi(1:6)
eulerPhi(36)
eulerPhi(sym(97))
Output
ans = 1×6

     1     1     2     2     4     2
ans = 12
ans = 96

See also

factor primes isprime gcd isPrimitiveRoot