Prime Factorization

Table of Contents

  1. What is Prime Factorization
  2. Step-by-Step Algorithm: Trial Division
  3. Famous Algorithms for Large Numbers
  4. Key Formulas and Properties
  5. Real-World Applications
  6. Famous Numbers and Stories
  7. Primality Testing
  8. Related Tools
  9. FAQ

1. What is Prime Factorization

Prime factorization is the process of expressing a positive integer greater than 1 as a product of prime numbers. For example, 360 = 23 × 32 × 5. This is not an optional mathematical trick โ€” it reveals the fundamental structure of an integer, much like a molecular formula reveals a substance's atomic composition.

The Fundamental Theorem of Arithmetic

Theorem

Every integer greater than 1 can be represented uniquely as a product of prime numbers, up to the order of the factors.

This theorem has two critical parts:

Historical Context

Euclid (~300 BC) first established existence in his Elements, Book IX: every composite number is divisible by some prime. However, he did not prove uniqueness.

The rigorous proof of uniqueness came nearly two millennia later, when Carl Friedrich Gauss published it in his landmark 1801 work Disquisitiones Arithmeticae. This treatise laid the foundation of modern number theory.

Why Does Uniqueness Matter?

If prime factorization were not unique, the entire edifice of number theory would collapse. For instance:

In fact, in certain algebraic structures (such as Z[√-5]), unique factorization genuinely fails โ€” this is precisely what motivated Ernst Kummer and Richard Dedekind to develop ideal theory in the 19th century.

2. Step-by-Step Algorithm: Trial Division

Trial division is the oldest and most intuitive prime factorization algorithm. Its core idea is beautifully simple: start with the smallest prime and try dividing repeatedly.

Algorithm Steps

  1. Start with the smallest prime, p = 2.
  2. If n is divisible by p (n mod p = 0), then p is a prime factor of n. Record p and set n = n / p.
  3. Repeat step 2 until n is no longer divisible by p.
  4. Advance p to the next prime (3, 5, 7, 11, ...) and return to step 2.
  5. Stop when p × p > n. If n > 1 at this point, then n itself is a prime factor.

Why We Only Need to Check Up to √n

This is a critical optimization with a clean mathematical proof:

Proof

Suppose n is composite, i.e., n = a × b where 1 < a ≤ b < n.
If a > √n, then b ≥ a > √n, so a × b > √n × √n = n โ€” a contradiction.
Therefore a ≤ √n. The smallest non-trivial factor of n never exceeds √n.

This means if no factor is found in the range 2 to √n, then n must be prime. This reduces the search space from n to √n.

Time Complexity

Trial division runs in O(√n) time. For n = 1012, we check at most ~106 divisors โ€” modern computers handle this in milliseconds. But for n = 10100 (like RSA keys), √n = 1050, which no supercomputer could finish within the age of the universe.

Code Implementation (JavaScript)

function trialDivision(n) { const factors = []; for (let p = 2; p * p <= n; p++) { while (n % p === 0) { factors.push(p); n = Math.floor(n / p); } } if (n > 1) factors.push(n); return factors; } // trialDivision(360) โ†’ [2, 2, 2, 3, 3, 5]

Complete Walkthrough with 360

StepCurrent nDivisor pOperationResult
13602360 ÷ 2 = 180Record factor 2
21802180 ÷ 2 = 90Record factor 2
390290 ÷ 2 = 45Record factor 2
445245 mod 2 ≠ 0Skip, p → 3
545345 ÷ 3 = 15Record factor 3
615315 ÷ 3 = 5Record factor 3
7533 × 3 = 9 > 5Stop, n=5 is prime

Result: 360 = 23 × 32 × 5

3. Famous Algorithms for Large Numbers

Trial division works well for small numbers, but when numbers grow to dozens or hundreds of digits, we need far more efficient algorithms. Here are the key algorithms in historical order.

AlgorithmInventorYearComplexityBest For
Trial Division(ancient)โ€”O(√n)Small numbers (< 1012)
Sieve of EratosthenesEratosthenes~240 BCO(n log log n)Generating prime tables
Fermat's FactorizationPierre de Fermat1643Depends on factor gapFactors close to √n
Pollard's RhoJohn Pollard1975O(n1/4)Medium-sized numbers
Quadratic SieveCarl Pomerance1981Sub-exponentialLarge numbers (< 100 digits)
General Number Field SievePollard / Lenstra et al.1993Sub-exponential (fastest)Largest numbers (> 100 digits)

Sieve of Eratosthenes

Inventor: Eratosthenes of Cyrene, circa 240 BC โ€” also famous for accurately measuring the Earth's circumference.

Idea: Rather than testing individual numbers for primality, systematically sieve out all composites. Starting from 2, mark all multiples of 2 as composite; find the next unmarked number (3) and mark its multiples; and so on. All remaining unmarked numbers are prime.

Use in factorization: We can pre-generate a table of primes using the sieve, then perform trial division using only primes, skipping all composite divisors.

Fermat's Factorization

Inventor: Pierre de Fermat, 1643.

Core idea: Express n as a difference of two squares: n = a2 - b2 = (a + b)(a - b). Start at a = ⌈√n⌉ and check whether a2 - n is a perfect square.

Strength: When n's two factors are close to √n (e.g., the two prime factors of an RSA semiprime are similar in size), Fermat's method finds the factorization extremely quickly. This is precisely why RSA requires its two prime factors to differ significantly in magnitude.

Pollard's Rho Algorithm

Inventor: John Pollard, published 1975.

Core idea: Exploit a pseudo-random sequence and the birthday paradox. Define an iteration function f(x) = (x2 + c) mod n, maintain two iterators at different speeds (Floyd's cycle detection), and compute the GCD of their difference with n. When the GCD is neither 1 nor n, we have found a non-trivial factor.

Complexity: Expected O(n1/4) โ€” vastly better than trial division's O(n1/2). Excellent for numbers in the 20-25 digit range.

Code: Pollard's Rho (JavaScript)

function pollardRho(n) { if (n % 2 === 0) return 2; let x = 2, y = 2, c = 1, d = 1; while (d === 1) { x = (x * x + c) % n; // tortoise: one step y = (y * y + c) % n; y = (y * y + c) % n; // hare: two steps d = gcd(Math.abs(x - y), n); } return d === n ? null : d; // null โ†’ retry with different c } function gcd(a, b) { while (b) { [a, b] = [b, a % b]; } return a; }

Quadratic Sieve and General Number Field Sieve (GNFS)

The Quadratic Sieve, proposed by Carl Pomerance in 1981, was the first practical sub-exponential factoring algorithm. It works by finding "smooth numbers" (numbers with only small prime factors) in a sieving interval, then constructing a system of linear equations to extract factors of n.

The General Number Field Sieve (GNFS) is the fastest known algorithm for factoring large integers. It operates in algebraic number fields using lattice reduction and sophisticated sieving strategies. In 2020, GNFS successfully factored RSA-250 (829 binary digits, 250 decimal digits), setting the current world record.

4. Key Formulas and Properties

Once we have the prime factorization n = p1a1 × p2a2 × ... × pkak, we can directly compute many important number-theoretic functions.

Divisor Count Function τ(n)

τ(n) = (a1 + 1)(a2 + 1) ··· (ak + 1)

Why does this formula work? (Combinatorial argument)

Any divisor d of n has the form d = p1b1 × p2b2 × ... × pkbk, where 0 ≤ bi ≤ ai. For each prime factor pi, the exponent bi has (ai + 1) choices (from 0 through ai). By the multiplication principle, the total number of divisors equals the product of choices for each prime factor.

Example: 360 = 23 × 32 × 51, so τ(360) = (3+1)(2+1)(1+1) = 4 × 3 × 2 = 24.

Sum of Divisors Function σ(n)

σ(n) = ∏i=1..k (piai+1 - 1) / (pi - 1)

Derivation: For each prime factor pi, its contribution to divisors is pi0 + pi1 + ... + piai. This is a geometric series with sum (piai+1 - 1) / (pi - 1). Since the sum-of-divisors function is multiplicative, the total equals the product of each prime's contribution.

Example: σ(360) = (24-1)/(2-1) × (33-1)/(3-1) × (52-1)/(5-1) = 15 × 13 × 6 = 1170.

Euler's Totient Function φ(n)

φ(n) = n × ∏i=1..k (1 - 1/pi)

Who: Leonhard Euler, introduced in 1763. Euler's totient φ(n) counts the number of positive integers from 1 to n that are coprime to n.

Why it matters:

Example: φ(360) = 360 × (1 - 1/2) × (1 - 1/3) × (1 - 1/5) = 360 × 1/2 × 2/3 × 4/5 = 96.

Formula Summary Table

FunctionFormulaExample for 360
Divisor Count τ(n)∏(ai + 1)24
Sum of Divisors σ(n)∏(piai+1 - 1)/(pi - 1)1170
Euler's Totient φ(n)n × ∏(1 - 1/pi)96
Liouville's λ(n)(-1)∑ai(-1)6 = 1
von Mangoldt Λ(n)log p if n = pk, else 00 (not a prime power)

5. Real-World Applications

RSA Cryptography

RSA (Rivest, Shamir, Adleman, 1977) is a cornerstone of modern internet security. Its security relies entirely on the difficulty of factoring large integers.

How it works: Choose two large primes p and q (each ~150-300 digits), compute n = p × q. The public key contains n, but recovering p and q from n (i.e., factoring n) is computationally infeasible. Decryption requires φ(n) = (p-1)(q-1), and computing φ(n) is equivalent to factoring n.

Current record: RSA-250 (250 decimal digits, 829 binary digits) was factored in 2020, requiring approximately 2,700 CPU-years of computation. RSA-2048 (617 decimal digits) used in practice is considered safe with current technology.

Quantum threat: Peter Shor's 1994 quantum algorithm can factor large integers in polynomial time. If large-scale quantum computers become reality, RSA will be broken โ€” this drives active research in post-quantum cryptography.

GCD and LCM Computation

Given the prime factorizations of two numbers:

Example: 360 = 23 × 32 × 5, and 150 = 2 × 3 × 52

Simplifying Fractions

Reducing a fraction a/b to lowest terms is essentially computing GCD(a, b) and dividing both by it. Prime factorization lets us visually see which factors cancel. For example: 120/360 = (23×3×5) / (23×32×5) = 1/3.

Applications in Number Theory

6. Famous Numbers and Stories

Mersenne Primes

Primes of the form Mp = 2p - 1 are named after French friar Marin Mersenne (1588-1648). In 1644, Mersenne claimed to list all p ≤ 257 for which Mp is prime โ€” although his list had several errors, it inspired centuries of research.

Why they're special: Mersenne primes are the source of the largest known primes. The current record holder is 282,589,933 - 1 (approximately 24.9 million digits), discovered by the GIMPS (Great Internet Mersenne Prime Search) project in 2018. GIMPS is a distributed computing project that anyone can contribute to.

Connection to perfect numbers: The Euclid-Euler theorem states that every Mersenne prime Mp yields an even perfect number 2p-1 × Mp.

Fermat Numbers

Numbers of the form Fn = 22n + 1. Fermat observed that F0=3, F1=5, F2=17, F3=257, F4=65537 are all prime, and conjectured that all Fermat numbers are prime.

The counterexample: Leonhard Euler proved in 1732 that F5 = 4,294,967,297 = 641 × 6,700,417, decisively refuting Fermat's conjecture. Every Fermat number tested since (F5 through F32) has been shown composite or remains of unknown primality. No Fermat prime beyond F4 has ever been found.

A surprising connection: Gauss proved that a regular n-gon can be constructed with compass and straightedge if and only if n is a power of 2 times a product of distinct Fermat primes. This is why the regular 17-gon (F2=17) is constructible but the regular 7-gon is not.

Twin Prime Conjecture

The conjecture states that there are infinitely many "twin primes" โ€” pairs of primes differing by 2, such as (3,5), (11,13), (29,31), (41,43).

This conjecture remains unproven. In 2013, Yitang Zhang proved there are infinitely many prime pairs differing by at most 70 million โ€” a major breakthrough. Subsequently, James Maynard and Terence Tao's Polymath project reduced this bound to 246. But the final gap from 246 to 2 remains out of reach.

Goldbach's Conjecture

In 1742, Prussian mathematician Christian Goldbach wrote to Euler proposing: every even number greater than 2 can be expressed as the sum of two primes. Examples: 4=2+2, 6=3+3, 8=3+5, 10=3+7=5+5, 100=3+97=11+89=17+83=29+71=41+59=47+53.

This is one of the oldest unsolved conjectures in mathematics. Computer verification has confirmed it for all even numbers up to 4 × 1018, but a rigorous proof remains elusive.

7. Primality Testing

Primality testing (determining whether a number is prime) is closely related to prime factorization but fundamentally different: deciding whether a number is prime is much easier than finding its factors.

Miller-Rabin Test

Inventors: Gary L. Miller (1976, deterministic version assuming the Generalized Riemann Hypothesis) and Michael O. Rabin (1980, probabilistic version).

Principle: Based on the contrapositive of Fermat's little theorem. Write n-1 as 2s × d, pick a random base a, check whether ad mod n equals 1 or -1, and whether -1 appears in the sequence a2d, a4d, ...

Advantage: Each round has error probability at most 1/4. After k rounds, error probability ≤ 4-k. With 20 rounds, error probability is ~10-12, reliable enough for practical use. Time complexity: O(k × log2 n × log log n).

Usage: The standard method for finding large primes in RSA key generation. Used in OpenSSL, GnuPG, and other cryptographic libraries.

AKS Primality Test

Inventors: Manindra Agrawal, Neeraj Kayal, Nitin Saxena, 2002 (IIT Kanpur, India).

Historical Significance

AKS was the first proven deterministic polynomial-time primality test. It settled a question mathematicians had asked for over two millennia: is primality testing in class P? The answer is yes.

Principle: Based on a generalized Fermat's little theorem: n is prime if and only if the polynomial identity (x + a)n ≡ xn + a (mod n) holds. AKS cleverly reduces this condition โ€” which naively requires checking exponentially many polynomial coefficients โ€” to a form verifiable in polynomial time.

Complexity: O(log6 n) (original), improved to O(log3 n) under certain conjectures.

Practical use: Although theoretically groundbreaking, AKS is much slower than Miller-Rabin in practice, so cryptographic implementations still prefer Miller-Rabin. The value of AKS lies in its theoretical significance โ€” it establishes the exact position of primality testing in computational complexity theory.

Primality Testing Comparison

AlgorithmTypeComplexityDeterministic?Practical Speed
Trial DivisionDeterministicO(√n)YesFast for small n
Miller-RabinProbabilisticO(k log2 n)No*Very fast
AKSDeterministicO(log6 n)YesSlower

*Miller-Rabin's deterministic variant requires the Generalized Riemann Hypothesis.

9. FAQ

Is 1 a prime number?

No. By modern convention, a prime must be greater than 1 and have exactly two factors: 1 and itself. Excluding 1 from the primes is necessary to preserve the uniqueness of prime factorization in the Fundamental Theorem of Arithmetic. If 1 were prime, then 6 = 2 × 3 = 1 × 2 × 3 = 12 × 2 × 3 = ..., destroying uniqueness.

Can prime factorization be applied to negative numbers or zero?

The Fundamental Theorem of Arithmetic applies only to positive integers greater than 1. For negative numbers, you can take the absolute value, factor it, and prepend a -1 sign. Zero cannot be prime-factored because it is divisible by every positive integer.

Is integer factorization NP-hard?

Surprisingly, no โ€” at least, it has not been proven to be. Integer factorization is believed to lie in the intersection of NP and co-NP, but is unlikely to be NP-complete (if it were, NP = co-NP, contradicting mainstream conjectures). Shor's quantum algorithm places it in BQP, suggesting it may be "easier" than classical NP-hard problems.

How large a number can this calculator handle?

This tool uses JavaScript BigInt for arbitrary-precision integer arithmetic. However, since it uses trial division, numbers beyond ~15 digits may be noticeably slow. For very large numbers (RSA-scale), use specialized number theory software like PARI/GP, Mathematica, or SageMath.

Will quantum computers make prime factorization easy?

In theory, yes. Peter Shor's 1994 quantum factoring algorithm runs in O((log n)3) time, vastly faster than any known classical algorithm. However, as of 2026, practical quantum computers cannot yet factor numbers beyond a few dozen digits. IBM, Google, and others are actively developing quantum hardware, but large-scale fault-tolerant quantum computers remain years away. Meanwhile, NIST finalized post-quantum cryptography standards in 2024 to prepare for the eventual quantum threat.