Purpose: Factor large integers in sub-exponential time — the fastest known method for numbers of 60-100 digits, and the second fastest overall after the general number field sieve.
The Idea: congruence of squares
If you can find with
then is a non-trivial factor, since but divides neither factor. This is Fermat’s old idea; QS is a systematic way to manufacture such a congruence.
Algorithm
- Pick a factor base : the primes up to a bound for which is a quadratic residue (checked with the Legendre symbol — otherwise can never divide ).
- Define . For small , is small, so it is more likely to be -smooth (factor completely over ).
- Sieve. Rather than trial-dividing each , note that exactly on an arithmetic progression of (found by solving with Tonelli-Shanks). March through an array subtracting at those positions; entries whose accumulated logs match are smooth. This sieving step is the entire speed advantage.
- Collect smooth relations. Each gives an exponent vector mod 2 over the factor base.
- Linear algebra. Find a subset whose exponent vectors sum to zero mod 2 — a null-space vector of a matrix over . With vectors in a -dimensional space, one always exists.
- That subset’s product is a perfect square on both sides, giving . Compute ; retry with another null-space vector if it is trivial (happens about half the time).
Complexity
- Time: — sub-exponential but super-polynomial
- The optimal factor base size is about primes
- The linear algebra is done with block Lanczos or Wiedemann over , not Gaussian elimination, because the matrix is huge and sparse
Where each method wins
| Digits of | Best method |
|---|---|
| ≤ 19 (64-bit) | Pollard’s rho |
| 20 – 60 | ECM (especially if a factor is small) |
| 60 – 100 | Quadratic Sieve |
| > 100 | General Number Field Sieve, |
Not a competitive programming tool
No contest asks you to factor a 90-digit number. QS is here for completeness and because the congruence of squares idea — build relations, then do linear algebra over — recurs in problems about XOR bases and subset-parity constructions.
Variants / Use Cases
- Multiple Polynomial QS (MPQS) — use many polynomials so the values stay small over a wider range; the standard practical version
- Self-Initialising QS (SIQS) — cheap polynomial switching, the fastest QS variant
- Lenstra ECM — better when a medium factor exists; QS’s cost depends only on , ECM’s on the smallest factor
- Integer Factorization — the topic page
- Wiedemann / block Lanczos — the sparse linear algebra QS depends on