Miller-Rabin Primality Test
Probabilistic primality test. Fast for large numbers. k rounds → error probability ≤ 4^(-k).
Visualization
Interactive visualization for Miller-Rabin Primality Test
Interactive visualization with step-by-step execution
Implementation
1function millerRabin(n: number, k: number = 5): boolean {
2 if (n < 2) return false;
3 if (n === 2 || n === 3) return true;
4 if (n % 2 === 0) return false;
5
6 let d = n - 1, s = 0;
7 while (d % 2 === 0) {
8 d /= 2;
9 s++;
10 }
11
12 for (let i = 0; i < k; i++) {
13 const a = 2 + Math.floor(Math.random() * (n - 3));
14 let x = modPow(a, d, n);
15
16 if (x === 1 || x === n - 1) continue;
17
18 let composite = true;
19 for (let r = 0; r < s - 1; r++) {
20 x = modPow(x, 2, n);
21 if (x === n - 1) {
22 composite = false;
23 break;
24 }
25 }
26
27 if (composite) return false;
28 }
29 return true;
30}Deep Dive
Theoretical Foundation
If n is prime: either a^d ≡ 1 (mod n) or a^(2^r × d) ≡ -1 (mod n) for some r. Test with multiple random bases. Deterministic for n < 3,317,044,064,679,887,385,961,981.
Complexity
Time
O(k log³ n)
O(k log³ n)
O(k log³ n)
Space
O(1)
Applications
Industry Use
RSA key generation (finding large primes)
Cryptographic protocol implementation
Digital signature algorithms
Blockchain proof-of-work systems
Random prime generation for security
Primality certificates in number theory
Factorization algorithm preprocessing
Mathematical research and verification
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¹.