# TAOCP 4.5.4 Factoring into Primes

> Study guide to TAOCP 4.5.4 Factoring into Primes. Methods for factoring integers and testing primality: trial division, Fermat's method, Pollard's rho…

Updated October 20, 2026.

## Summary {#summary}

TAOCP 4.5.4 Factoring into Primes: Methods for factoring integers and testing primality: trial division, Fermat's method, Pollard's rho, probabilistic primality testing, and the connection to public-key cryptography (RSA), plus a look at more advanced sieving methods. Budget about 10 hours; do exercises 1, 5 and 8.

## Key facts {#key-facts}

- Week 84 of 145 on the cracked.guide path, in [TAOCP Vol 2: Seminumerical Algorithms](https://cracked.guide/taocp/volume-2).
- Time budget: about 10 hours.
- 47 exercises in the section, rated 10 to M50; the guide picks 8.
- Picked exercises with Knuth's triangle for recommended: 8, 10, 19, 26 and 22.
- Before you start: 4.5.2 The Greatest Common Divisor and 4.3.2 Modular Arithmetic.

## Week 84 goals {#objectives}

By the end of week 84 you can:

- Implement trial division, Pollard's rho and a Miller-Rabin test.
- Trace Fermat's method on a number with two close factors.
- Explain why a probabilistic primality test can only say probably prime.

## This week's steps {#steps}

- Read §4.5.4 Factoring into Primes: Implement trial division, Pollard rho (with Floyd's cycle detection) and a Miller-Rabin primality test.
- Work the exercises (8 picked): Start with Ex 1, Ex 5, Ex 8; log what you solve below.
- Write down one thing you'd explain to someone: A few sentences in your own words in Your notes: the idea, why it works, an example. 30 words ticks this.

## Exercises {#exercises}

This section has 47 exercises, rated 10 to M50, and Knuth marks 16 of them with the triangle for recommended. Implement trial division, Pollard rho, Miller-Rabin and a sieve with 64-bit or bignum arithmetic and do these alongside.

Worth your time:

- 1 (rated 10): Why composite trial divisors never show up in the output.
- 5 (rated 21): Fermat's method by hand on a five-digit number.
- 8 (rated 23, recommended): The sieve of Eratosthenes and how to do it well. Implement it.
- 10 (rated M26, recommended): Selfridge's test for proving primality from the factors of n - 1.
- 19 (rated M25, recommended): Pollard's p - 1 idea. Another factoring method to implement.
- 26 (rated M25, recommended): Pocklington's primality certificate.
- 38 (rated 25): Find the largest 50-digit primes made mostly of one digit. A fun programming exercise with Miller-Rabin.
- 22 (rated M30, recommended): Bound the error rate of the probabilistic primality test. The stretch.

## Why it matters {#why}

Primality testing and the hardness of factoring are the foundation of RSA. Pollard rho is also a neat use of cycle detection.

## Before you start {#before}

- 4.5.2 The Greatest Common Divisor
- 4.3.2 Modular Arithmetic

## How to approach it {#approach}

Implement trial division, Pollard rho (with Floyd's cycle detection) and a Miller-Rabin primality test. Factor some 20-digit numbers. Read the advanced methods for the ideas only.

## Common traps {#traps}

- Overflow in modular multiplication. Use 128-bit intermediates or bignums.
- Treating a probabilistic test as deterministic.

## Links {#links}

- [Knuth's TAOCP page (editions, errata, MIX/MMIX)](https://www-cs-faculty.stanford.edu/~knuth/taocp.html)
- [Vol 2 errata (plain text, from Knuth)](https://www-cs-faculty.stanford.edu/~knuth/err2.textxt)
- [Wikipedia: Pollard's rho algorithm](https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm)
- [Wikipedia: Miller-Rabin primality test](https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test)
- [Wikipedia: RSA (cryptosystem)](https://en.wikipedia.org/wiki/RSA_(cryptosystem))
- [Hacker News: Idle Thoughts About Pollard Rho](https://news.ycombinator.com/item?id=17958215)

---

Source: https://cracked.guide/guide/knuth-w067-0
cracked.guide: A free, dated 145-week path to getting genuinely cracked at programming.
