Miller-Rabin Test (Basic)
Basic probabilistic primality test. Fast for large numbers, used in cryptography. Error probability: 4^(-k) where k = iterations.
Visualization
Interactive visualization for Miller-Rabin Test (Basic)
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 // Write n-1 as 2^r * d
7 let r = 0, d = n - 1;
8 while (d % 2 === 0) {
9 r++;
10 d = Math.floor(d / 2);
11 }
12
13 // Witness loop
14 for (let i = 0; i < k; i++) {
15 const a = 2 + Math.floor(Math.random() * (n - 3));
16 let x = modPow(a, d, n);
17
18 if (x === 1 || x === n - 1) continue;
19
20 let composite = true;
21 for (let j = 0; j < r - 1; j++) {
22 x = modPow(x, 2, n);
23 if (x === n - 1) {
24 composite = false;
25 break;
26 }
27 }
28
29 if (composite) return false;
30 }
31
32 return true;
33}
34
35function modPow(base: number, exp: number, mod: number): number {
36 let result = 1;
37 base %= mod;
38 while (exp > 0) {
39 if (exp % 2 === 1) result = (result * base) % mod;
40 base = (base * base) % mod;
41 exp = Math.floor(exp / 2);
42 }
43 return result;
44}Deep Dive
Theoretical Foundation
Based on Fermat's Little Theorem and strong pseudoprime concept. For odd n, write n-1 = 2^r × d. Test if a^d ≡ 1 (mod n) or a^(2^i×d) ≡ -1 (mod n) for some i. Repeat with k random bases.
Complexity
Time
O(k log³ n)
O(k log³ n)
O(k log³ n)
Space
O(1)
Applications
Industry Use
RSA key generation (large prime testing)
Cryptographic protocol implementations
Digital signature algorithms
Blockchain and cryptocurrency systems
Secure communication protocols (TLS/SSL)
Mathematical software libraries
Prime number research and computation
Elliptic curve cryptography
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¹.