"how numbers are stored and used in computers"
Pollard's Rho algorithm is a probabilistic factorization algorithm that is particularly effective at finding small prime factors of composite numbers. It uses a cycle-finding algorithm to detect when a sequence of numbers begins to repeat, which can lead to the discovery of factors. This method is significantly faster than trial division for numbers with small prime factors.
The algorithm is based on the birthday paradox and uses a pseudo-random function to generate a sequence of numbers. When this sequence begins to cycle, we can use the cycle to find a non-trivial factor of the number. The algorithm uses Floyd's cycle-finding algorithm to detect the cycle efficiently.
The algorithm uses a polynomial function of the form:
where c is a constant and n is the number to be factored. The sequence generated by this function will eventually cycle, and when it does, we can find a factor by computing the greatest common divisor (GCD) of the difference between two numbers in the cycle and n.
The expected time complexity of Pollard's Rho algorithm is
code.js1function gcd(a, b) { 2 while (b !== 0) { 3 [a, b] = [b, a % b]; 4 } 5 return a; 6} 7 8function pollardRho(n) { 9 if (n === 1) return []; 10 if (isPrime(n)) return [n]; 11 12 function f(x) { 13 return (x * x + 1) % n; 14 } 15 16 let x = 2, y = 2, d = 1; 17 let c = 1; 18 19 while (d === 1) { 20 x = f(x); 21 y = f(f(y)); 22 d = gcd(Math.abs(x - y), n); 23 } 24 25 if (d === n) { 26 // If we found n itself, try a different starting point 27 return pollardRho(n); 28 } 29 30 // Recursively factor both parts 31 return [...pollardRho(d), ...pollardRho(n / d)]; 32} 33 34// Helper function to check if a number is prime 35function isPrime(n) { 36 if (n <= 1) return false; 37 if (n <= 3) return true; 38 if (n % 2 === 0 || n % 3 === 0) return false; 39 40 for (let i = 5; i * i <= n; i += 6) { 41 if (n % i === 0 || n % (i + 2) === 0) return false; 42 } 43 return true; 44}
Let's factorize the number 8051:
code.js1const result = pollardRho(8051); 2console.log(result); // [83, 97]
This means that 8051 = 83 × 97.
Several optimizations can improve the performance of Pollard's Rho algorithm:
The main limitations of Pollard's Rho algorithm are:
Pollard's Rho algorithm is particularly useful in: