Search

Search papers and lessons

Integer Factorization Problem

Level 3 · 12 min read

Splitting a whole number into primes: multiplying is quick, but undoing a large product has no known fast method, and RSA encryption relies on that gap.

On this page

  • Colour = topic mix
  • Discrete math
  • Algorithms
  • Security & cryptography
  • Calculus
  • Sign in to see your progress

TL;DR

Integer factorization asks for the prime factors of a whole number: given nn, find primes whose product is nn. Multiplying a few primes together is quick. Running the process backwards, from a large product to its primes, has no known fast method, and nobody has proven that none exists. The RSA scheme for digital signatures and public-key encryption relies on that gap.

Where you'll see it: in any public-key system where the public number is built from two secret primes, and in the background of every discussion of why some computations are considered hard.

This lesson explains a mathematical problem and why it is believed to be hard. It is not advice about key sizes or about using cryptographic software.

Intuition

Take two primes, 53 and 61. Multiplying them takes a minute by hand and gives 3233. Now reverse the question. Someone gives you 3233 and asks which two whole numbers multiply to make it. There is no formula to read off the answer. You have to search: test small divisors until one divides evenly. For 3233 that search is short. The question this lesson answers is what happens to the search when the number has 100 digits instead of four.

flowchart LR
  A["53 and 61"] -->|"multiply: quick"| B["3233"]
  B -->|"factor: a search"| A

Why primes are the target

Every whole number above 1 is a product of primes in exactly one way, apart from the order of the factors. Primes are the irreducible pieces, so factoring means finding them. Once you have the primes of nn, you know every divisor of nn. A number with exactly two prime factors, like 3233, is the case that matters most for cryptography.

The asymmetry is the point

The defender builds a public number by multiplying two secret primes. Multiplying is cheap for the defender. The attacker has to undo the multiplication to recover the secret, and that must be expensive. This is the same shape as a one-way function: the forward direction is easy, the reverse is hard, and the security is the gap between the two costs.

Measure the cost in digits

A method's cost should be measured against the length of its input, which is the number of digits, not the size of the value. Adding one digit to nn makes the input one symbol longer, but for the search it multiplies the work by about 3.16 (the square root of 10). Doubling the number of digits squares the work. That kind of growth is what big-O analysis is built to describe.

The math

Definition

Definition: Integer factorization problem

Given an integer n>1n \gt 1, find primes p1,p2,…,pkp_1, p_2, \dots, p_k, repetition allowed, with n=p1p2⋯pkn = p_1 p_2 \cdots p_k. The product on the right is the product of all the primes in the list, and kk is how many there are. By the fundamental theorem of arithmetic the list is unique up to order, so the answer is well defined. A weaker version asks for one nontrivial divisor dd, meaning d∣nd \mid n and 1<d<n1 \lt d \lt n, where d∣nd \mid n means n=d⋅qn = d \cdot q for some whole number qq. Finding one such dd splits the problem in two, and applying the same search to each piece recovers the full list.

Trial division needs only the square root

Claim: if n>1n \gt 1 is not prime, it has a divisor dd with 1<d≤n1 \lt d \le \sqrt{n}.

Proof: write n=abn = ab with 1<a≤b1 \lt a \le b. Then a2≤ab=na^2 \le ab = n, so a≤na \le \sqrt{n}. The smaller factor is always at most the square root.

Trial division uses this claim. Test d=2,3,4,…d = 2, 3, 4, \dots up to ⌊n⌋\lfloor \sqrt{n} \rfloor, and stop at the first dd that divides nn. Testing a composite dd is wasteful but harmless, since its prime factors would already have been caught. The worst-case cost is therefore about n\sqrt{n} divisions.

A number with dd digits satisfies n<10dn \lt 10^d, so

n<10d/2.\sqrt{n} \lt 10^{d/2}.

Trial division takes at most about 10d/210^{d/2} divisions for a dd-digit number. The input has dd symbols, but the work is ten raised to a power proportional to dd.

2468101202000004000006000008000001000000number of digits in ndivisions
  • worst-case trial divisions
Trial division needs about 10 to the power d/2 divisions for a d-digit number. At 12 digits that is already a million.

Better methods

Trial division is the simplest method, and several faster ones exist. Each one exploits a different structure.

Fermat's method. Write n=x2−y2=(x−y)(x+y)n = x^2 - y^2 = (x - y)(x + y). Start at x=⌈n⌉x = \lceil \sqrt{n} \rceil and check whether x2−nx^2 - n is a perfect square y2y^2. If it is, x−yx - y and x+yx + y are factors. If n=pqn = pq with pp and qq close together, the value x=(p+q)/2x = (p + q)/2 is reached within a few steps.

Pollard's rho. Finds a prime factor pp in roughly p\sqrt{p} steps. Its cost depends on the smallest prime factor, not on the size of nn, so it is fast when nn has a small factor.

The quadratic sieve and the number field sieve. These are the fastest known general methods for large numbers with no small factors. Their running time is subexponential:

exp⁡ ⁣(c (ln⁡n)1/3 (ln⁡ln⁡n)2/3),\exp\!\left(c\,(\ln n)^{1/3}\,(\ln \ln n)^{2/3}\right),

where exp⁡\exp is the exponential function, ln⁡\ln is the natural logarithm, and cc is a constant (about 1.9 for the number field sieve). In terms of the digit count dd, this grows faster than any fixed power of dd but slower than any fixed base raised to dd. It is far better than trial division, and still far too slow to factor a random well-chosen number of several hundred digits.

No classical algorithm is known to factor in time that is a polynomial in dd. This is an open question, not a theorem.

Quantum computers

In 1994 Peter Shor gave an algorithm that factors in time polynomial in dd, provided it runs on a sufficiently large quantum computer. Current machines are far too small for that. The possibility is the reason cryptographers are developing post-quantum schemes whose security does not rest on factoring.

Worked examples

Example
Tiny: factor 91 by trial division

The square root of 91 is about 9.5, so test divisors up to 9.

  1. Divide 91 by 2. The remainder is 1, so no.
  2. Divide 91 by 3. The digit sum is 9+1=109 + 1 = 10, which is not a multiple of 3, so no.
  3. Divide 91 by 5. The last digit is 1, so no.
  4. Divide 91 by 7. 7×13=917 \times 13 = 91, so yes.

Interpretation: 7 and 13 are both prime, so 91=7×1391 = 7 \times 13 is the full factorization. Four tests were enough.

Example
Standard: factor 3233 by trial division

The square root of 3233 is about 56.9, since 562=313656^2 = 3136 and 572=324957^2 = 3249. Only primes need testing up to 56, because a composite divisor always has a prime factor that was already tested. Those primes are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 and 53, which is 16 primes.

  1. Divide 3233 by each of the first 15 primes. None leaves remainder 0.
  2. Divide by the 16th prime, 53. The quotient is 61, so 3233=53×613233 = 53 \times 61.
  3. Check the product: 53×61=3180+53=323353 \times 61 = 3180 + 53 = 3233.

Interpretation: 53 and 61 are both prime, so they are the factorization. The search took 16 divisions. For a 100-digit number the same method would need about 105010^{50} divisions, because the square root of 1010010^{100} is 105010^{50}.

Example
Realistic: why close primes are a weakness

Use Fermat's method on the same n=3233n = 3233, starting at x=⌈3233⌉=57x = \lceil \sqrt{3233} \rceil = 57.

  1. Compute x2−n=572−3233=3249−3233=16x^2 - n = 57^2 - 3233 = 3249 - 3233 = 16.
  2. Since 16=4216 = 4^2 is a perfect square, take y=4y = 4.
  3. Then n=x2−y2=(57−4)(57+4)=53×61n = x^2 - y^2 = (57 - 4)(57 + 4) = 53 \times 61.

Interpretation: one step was enough. The reason is that 53 and 61 are close: their average, 57, is already the first candidate. Two primes chosen independently at random from a large range are almost never this close, but a key generator that picks primes carelessly can produce exactly this pattern, and then the public number is easy to split.

Example
Realistic: what trial division costs at cryptographic sizes

Public-key systems use numbers with hundreds of digits. Consider trial division on three sizes, assuming a machine that tests a billion divisors per second.

  1. A 30-digit number: n<1015\sqrt{n} \lt 10^{15} divisions. At 10910^9 per second that is 10610^6 seconds, or about 12 days.
  2. A 60-digit number: n<1030\sqrt{n} \lt 10^{30} divisions, which takes 102110^{21} seconds. A year is about 3.2×1073.2 \times 10^7 seconds, so this is about 3×10133 \times 10^{13} years.
  3. A 617-digit number, the size of a 2048-bit RSA modulus: up to 10308.510^{308.5} divisions, roughly 3×103083 \times 10^{308}.

Interpretation: adding 30 digits multiplies the time by 101510^{15}. Trial division is hopeless at 60 digits. The better methods above reach much larger sizes, which is why the sizes used in practice are chosen with the best known algorithms in mind, not with trial division in mind.

Tradeoffs and failure modes

Factoring's difficulty is an assumption supported by decades of failed attempts to find a fast method. It is not a proof. The practical cost depends on the size of the number, the structure of its factors, and the algorithm used, and all three change over time.

SituationProblemBetter choice
The number is small, below about 101210^{12}Trial division finishes in secondsJust divide
The number has a small prime factorTrial division finds it, but the rest of the search is still hardStrip small primes first, then try Pollard's rho
The two primes are close togetherFermat's method splits the number in a few stepsChoose primes that are far apart and independently random
Large product of two big primes, classical computerBest known methods are subexponential but still very longUse the modulus size that current cryptographic guidance recommends
A large quantum computer existsShor's algorithm runs in polynomial timePost-quantum schemes

Some practical points:

  • Hardness is conjectural. Factoring is not known to be NP-complete, and no proof of its hardness exists. Its security rests on the absence of a known fast method.
  • Prime choice matters as much as size. A large modulus built from badly chosen primes can be split by Fermat's method or by a small-factor search.
  • A record is a data point, not a threshold. A record factorization shows what a large computation achieved on one number. It does not measure the risk to a particular key, which depends on the algorithm and the computing power available.
  • Discrete logarithms are an alternative. The discrete logarithm problem is a different hard problem that underlies Diffie–Hellman key exchange and some signature schemes. It does not reduce to factoring, and Shor's algorithm also breaks it on a large quantum computer.

History

Trial division is as old as the idea of primes. Fermat described his factoring method in letters in the 1600s, around the time of his work on number theory. In the Disquisitiones Arithmeticae (1801), Gauss treated factoring as an important and difficult problem and discussed methods for it.

Factoring became a practical research field in the 1970s. John Pollard published his rho method in 1975. The quadratic sieve appeared in the early 1980s, and the number field sieve in the early 1990s became the fastest known general method.

In 1978 Rivest, Shamir and Adleman published the RSA scheme. It made factoring the basis of public-key encryption and signatures, and it turned a classical question into a security assumption. In 1994 Shor showed that a quantum computer could factor in polynomial time. In 2020 a 250-digit RSA challenge number was factored after a large distributed computation, a record that shows how far the best methods have come.

The problem caught on because decades of attacks failed to find a polynomial-time method, and each improvement needed deeper mathematics rather than a simple shortcut. That record of resistance is why it became a standard assumption.

Recap

Every whole number is a product of primes in exactly one way, so factoring means finding those primes. Multiplying is quick. Trial division needs about n\sqrt{n} steps, which is exponential in the digit count. Better methods help in special cases (Fermat for close primes, Pollard's rho for small factors) and in general (the number field sieve, subexponential but still very slow for well-chosen large numbers). The gap between multiplying and factoring is the basis of Public-Key Cryptography, and the same kind of asymmetry appears in the One-Way Function and the Discrete Logarithm Problem.

Check yourself

QuestionWhy does trial division only need to check divisors up to the square root of n?Show answer

Suppose nn has a divisor dd with 1<d<n1 \lt d \lt n, and write n=d⋅qn = d \cdot q. Then qq is also a divisor in that range. If both dd and qq were larger than n\sqrt{n}, their product would be larger than nn, which is impossible. So at least one of them is at most n\sqrt{n}, and the search can stop there.

QuestionA number has 40 digits. Roughly how many divisions does worst-case trial division need?Show answer

Since n<1040n \lt 10^{40}, we get n<1020\sqrt{n} \lt 10^{20}. Worst-case trial division needs about 102010^{20} divisions, which is 100 quintillion.

QuestionFermat's method splits n=3233n = 3233 in one step. What property of the two primes makes this possible, and why should an RSA key avoid it?Show answer

The primes are close together. Their average, 57, is the first square-root candidate ⌈n⌉\lceil \sqrt{n} \rceil, so x2−nx^2 - n is a perfect square right away. An RSA key should avoid this, because an attacker who can see that the primes are close can factor the public modulus quickly, however large it is.

QuestionSomeone says: a 250-digit number was factored in 2020, so a 617-digit RSA modulus is safe forever. What is wrong with this reasoning?Show answer

Two things. A factoring record shows what one large computation achieved on one number, and it does not measure the risk to a particular key, which depends on the algorithm and the computing power available. And "forever" ignores Shor's algorithm: a sufficiently large quantum computer would factor a 617-digit number in polynomial time, so the long-term risk is a different question from today's record.

Sources