Prime Factorization (Trial Division)
Basic prime factorization using trial division. Decompose number into product of primes. Simple method suitable for small to medium numbers.
Visualization
Interactive visualization for Prime Factorization (Trial Division)
Interactive visualization with step-by-step execution
Implementation
1function primeFactorization(n: number): number[] {
2 const factors: number[] = [];
3
4 // Handle 2s
5 while (n % 2 === 0) {
6 factors.push(2);
7 n = Math.floor(n / 2);
8 }
9
10 // Handle odd factors
11 for (let i = 3; i * i <= n; i += 2) {
12 while (n % i === 0) {
13 factors.push(i);
14 n = Math.floor(n / i);
15 }
16 }
17
18 // If n is prime > 2
19 if (n > 2) factors.push(n);
20
21 return factors;
22}
23
24// With count
25function primeFactorsWithCount(n: number): Map<number, number> {
26 const factors = new Map<number, number>();
27
28 let count = 0;
29 while (n % 2 === 0) {
30 count++;
31 n = Math.floor(n / 2);
32 }
33 if (count > 0) factors.set(2, count);
34
35 for (let i = 3; i * i <= n; i += 2) {
36 count = 0;
37 while (n % i === 0) {
38 count++;
39 n = Math.floor(n / i);
40 }
41 if (count > 0) factors.set(i, count);
42 }
43
44 if (n > 2) factors.set(n, 1);
45
46 return factors;
47}Deep Dive
Theoretical Foundation
Every integer > 1 can be uniquely represented as product of primes (Fundamental Theorem of Arithmetic). Trial division: divide by primes/numbers up to √n. For each factor found, divide repeatedly.
Complexity
Time
O(log n)
O(√n)
O(√n)
Space
O(log n)
Applications
Industry Use
RSA cryptography (breaking weak keys)
Finding perfect squares and cubes
Simplifying mathematical expressions
Calculating number of divisors
Solving Diophantine equations
Computer algebra systems
Pollard's rho algorithm preprocessing
Mathematical competition problems
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¹.