Euler's Totient Function φ(n)
Count integers ≤ n coprime to n. φ(n) = n × Π(1 - 1/p) for prime factors p.
Visualization
Interactive visualization for Euler's Totient Function φ(n)
Interactive visualization with step-by-step execution
Implementation
1function eulerTotient(n: number): number {
2 let result = n;
3
4 for (let p = 2; p * p <= n; p++) {
5 if (n % p === 0) {
6 while (n % p === 0) n /= p;
7 result -= result / p;
8 }
9 }
10
11 if (n > 1) result -= result / n;
12 return Math.floor(result);
13}
14
15function totientRange(n: number): number[] {
16 const phi = Array.from({length: n + 1}, (_, i) => i);
17
18 for (let p = 2; p <= n; p++) {
19 if (phi[p] === p) { // p is prime
20 for (let k = p; k <= n; k += p) {
21 phi[k] -= phi[k] / p;
22 }
23 }
24 }
25 return phi;
26}Deep Dive
Theoretical Foundation
φ(n) = count of k where gcd(k,n)=1. For prime p: φ(p)=p-1. Multiplicative: φ(ab)=φ(a)φ(b) if gcd(a,b)=1. Formula: φ(n) = n × Π(1-1/p).
Complexity
Time
O(√n) single, O(n log log n) sieve
O(√n)
O(√n)
Space
O(1) single, O(n) sieve
Applications
Industry Use
RSA cryptography (computing φ(n) for key generation)
Carmichael function computation
Primitive root finding in number theory
Cycle length in modular arithmetic
Group theory (order of multiplicative groups)
Cryptographic protocol design
Perfect number research
Diophantine equation solving
Use Cases
Related Algorithms
GCD (Euclidean Algorithm)
Compute the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm. Dating back to around 300 BC and appearing in Euclid's Elements, it's one of the oldest algorithms still in common use. The algorithm is based on the principle that GCD(a,b) = GCD(b, a mod b) and is remarkably efficient with O(log min(a,b)) time complexity. The GCD is the largest positive integer that divides both numbers without remainder.
LCM (Least Common Multiple)
Calculate the Least Common Multiple (LCM) of two integers - the smallest positive integer that is divisible by both numbers. The LCM is intimately related to the GCD through the formula: LCM(a,b) = |a×b| / GCD(a,b). This relationship allows us to compute LCM efficiently using the Euclidean algorithm for GCD, achieving O(log min(a,b)) time complexity instead of naive factorization methods.
Sieve of Eratosthenes
Ancient and highly efficient algorithm to find all prime numbers up to a given limit n. Invented by Greek mathematician Eratosthenes of Cyrene (276-194 BC), this sieve method systematically eliminates multiples of primes, leaving only primes in the array. With O(n log log n) time complexity, it remains one of the most practical algorithms for generating large lists of primes, vastly superior to trial division which runs in O(n² / log n) time.
Prime Factorization
Decompose a positive integer into its unique prime factor representation. Every integer greater than 1 can be expressed as a product of prime numbers in exactly one way (Fundamental Theorem of Arithmetic). This algorithm uses trial division optimized to check only up to √n, as any composite number must have a prime factor ≤ √n. Returns a map of prime factors to their powers, e.g., 360 = 2³ × 3² × 5¹.