Inmathematics,integer factorization is the decomposition of apositive integer into aproduct of integers. Every positive integer greater than 1 is either the product of two or more integerfactors greater than 1, in which case it is acomposite number, or it is not, in which case it is aprime number. For example,15 is a composite number because15 = 3 · 5, but7 is a prime number because it cannot be decomposed in this way. If one of the factors is composite, it can in turn be written as a product of smaller factors, for example60 = 3 · 20 = 3 · (5 · 4). Continuing this process until every factor is prime is calledprime factorization; the result is always unique up to the order of the factors by theprime factorization theorem.
To factorize a small integern using mental or pen-and-paper arithmetic, the simplest method istrial division: checking if the number is divisible by prime numbers2,3,5, and so on, up to thesquare root ofn. For larger numbers, especially when using a computer, various more sophisticated factorization algorithms are more efficient. A prime factorization algorithm typically involvestesting whether each factor is prime each time a factor is found.
When the numbers are sufficiently large, no efficient non-quantum integer factorizationalgorithm is known. However, it has not been proven that such an algorithm does not exist. The presumeddifficulty of this problem is important for the algorithms used incryptography such asRSA public-key encryption and theRSA digital signature.[1] Many areas of mathematics andcomputer science have been brought to bear on this problem, includingelliptic curves,algebraic number theory, and quantum computing.
Not all numbers of a given length are equally hard to factor. The hardest instances of these problems (for currently known techniques) aresemiprimes, the product of two prime numbers. When they are both large, for instance more than two thousandbits long, randomly chosen, and about the same size (but not too close, for example, to avoid efficient factorization byFermat's factorization method), even the fastest prime factorization algorithms on the fastest classical computers can take enough time to make the search impractical; that is, as the number of digits of the integer being factored increases, the number of operations required to perform the factorization on any classical computer increases drastically.
Many cryptographic protocols are based on the presumed difficulty of factoring large composite integers or a related problem – for example, theRSA problem. An algorithm that efficiently factors an arbitrary integer would renderRSA-basedpublic-key cryptography insecure.

By thefundamental theorem of arithmetic, every positive integer has a uniqueprime factorization. (By convention, 1 is theempty product.)Testing whether the integer is prime can be done inpolynomial time, for example, by theAKS primality test. If composite, however, the polynomial time tests give no insight into how to obtain the factors.
Given a general algorithm for integer factorization, any integer can be factored into its constituentprime factors by repeated application of this algorithm. The situation is more complicated with special-purpose factorization algorithms, whose benefits may not be realized as well or even at all with the factors produced during decomposition. For example, ifn = 171 ×p ×q wherep <q are very large primes,trial division will quickly produce the factors 3 and 19 but will takep divisions to find the next factor. As a contrasting example, ifn is the product of the primes13729,1372933, and18848997161, where13729 × 1372933 = 18848997157, Fermat's factorization method will begin with⌈√n⌉ = 18848997159 which immediately yieldsb =√a2 −n =√4 = 2 and hence the factorsa −b = 18848997157 anda +b = 18848997161. While these are easily recognized as composite and prime respectively, Fermat's method will take much longer to factor the composite number because the starting value of⌈√18848997157⌉ = 137292 fora is a factor of 10 from1372933.
Among theb-bit numbers, the most difficult to factor in practice using existing algorithms are thosesemiprimes whose factors are of similar size. For this reason, these are the integers used in cryptographic applications.
In 2019, a 240-digit (795-bit) number (RSA-240) was factored by a team of researchers includingPaul Zimmermann, utilizing approximately 900 core-years of computing power.[2] These researchers estimated that a 1024-bit RSA modulus would take about 500 times as long.[3]
The largest such semiprime yet factored wasRSA-250, an 829-bit number with 250 decimal digits, in February 2020. The total computation time was roughly 2700 core-years of computing using IntelXeon Gold 6130 at 2.1 GHz. Like all recent factorization records, this factorization was completed with a highly optimized implementation of thegeneral number field sieve run on hundreds of machines.
Noalgorithm has been published that can factor all integers inpolynomial time, that is, that can factor ab-bit numbern in timeO(bk) for some constantk. Neither the existence nor non-existence of such algorithms has been proved, but it is generally suspected that they do not exist.[4][5]
There are published algorithms that are faster thanO((1 + ε)b) for all positiveε, that is,sub-exponential. As of 2022[update], the algorithm with best theoretical asymptotic running time is thegeneral number field sieve (GNFS), first published in 1993,[6] running on ab-bit numbern in time:
For current computers, GNFS is the best published algorithm for largen (more than about 400 bits). For aquantum computer, however,Peter Shor discovered an algorithm in 1994 that solves it in polynomial time.Shor's algorithm takes onlyO(b3) time andO(b) space onb-bit number inputs. In 2001, Shor's algorithm was implemented for the first time, by usingNMR techniques on molecules that provide seven qubits.[7]
In order to talk aboutcomplexity classes such as P, NP, and co-NP, the problem has to be stated as adecision problem.
Decision problem (Integer factorization)—For every natural numbers and, doesn have a factor smaller thank besides 1?
It is known to be in bothNP andco-NP, meaning that both "yes" and "no" answers can be verified in polynomial time. An answer of "yes" can be certified by exhibiting a factorizationn =d(n/d) withd ≤k. An answer of "no" can be certified by exhibiting the factorization ofn into distinct primes, all larger thank; one can verify their primality using theAKS primality test, and then multiply them to obtainn. Thefundamental theorem of arithmetic guarantees that there is only one possible string of increasing primes that will be accepted, which shows that the problem is in bothUP and co-UP.[8] It is known to be inBQP because of Shor's algorithm.
The problem is suspected to be outside all three of the complexity classes P, NP-complete,[9] andco-NP-complete.It is therefore a candidate for theNP-intermediate complexity class.
In contrast, the decision problem "Isn a composite number?" (or equivalently: "Isn a prime number?") appears to be much easier than the problem of specifying factors ofn. The composite/prime problem can be solved in polynomial time (in the numberb of digits ofn) with theAKS primality test. In addition, there are severalprobabilistic algorithms that can test primality very quickly in practice if one is willing to accept a vanishingly small possibility of error. The ease ofprimality testing is a crucial part of theRSA algorithm, as it is necessary to find large prime numbers to start with.
A special-purpose factoring algorithm's running time depends on the properties of the number to be factored or on one of its unknown factors: size, special form, etc. The parameters which determine the running time vary among algorithms.
An important subclass of special-purpose factoring algorithms is theCategory 1 orFirst Category algorithms, whose running time depends on the size of smallest prime factor. Given an integer of unknown form, these methods are usually applied before general-purpose methods to remove small factors.[10] For example, naivetrial division is a Category 1 algorithm.
A general-purpose factoring algorithm, also known as aCategory 2,Second Category, orKraitchikfamily algorithm,[10] has a running time which depends solely on the size of the integer to be factored. This is the type of algorithm used to factorRSA numbers. Most general-purpose factoring algorithms are based on thecongruence of squares method.
In number theory, there are many integer factoring algorithms that heuristically have expectedrunning time
inlittle-o andL-notation.Some examples of those algorithms are theelliptic curve method and thequadratic sieve.Another such algorithm is theclass group relations method proposed by Schnorr,[11] Seysen,[12] and Lenstra,[13] which they proved only assuming the unprovedgeneralized Riemann hypothesis.
The Schnorr–Seysen–Lenstra probabilistic algorithm has been rigorously proven by Lenstra and Pomerance[14] to have expected running timeLn[1/2, 1+o(1)] by replacing the GRH assumption with the use of multipliers.The algorithm uses theclass group of positive binaryquadratic forms ofdiscriminantΔ denoted byGΔ.GΔ is the set of triples of integers(a,b,c) in which those integers are relative prime.
Given an integern that will be factored, wheren is an odd positive integer greater than a certain constant. In this factoring algorithm the discriminantΔ is chosen as a multiple ofn,Δ = −dn, whered is some positive multiplier. The algorithm expects that for oned there exist enoughsmooth forms inGΔ. Lenstra and Pomerance show that the choice ofd can be restricted to a small set to guarantee the smoothness result.
Denote byPΔ the set of all primesq withKronecker symbol(Δ/q) = 1. By constructing a set ofgenerators ofGΔ and prime formsfq ofGΔ withq inPΔ a sequence of relations between the set of generators andfq are produced.The size ofq can be bounded byc0(log|Δ|)2 for some constantc0.
The relation that will be used is a relation between the product of powers that is equal to theneutral element ofGΔ. These relations will be used to construct a so-called ambiguous form ofGΔ, which is an element ofGΔ of order dividing 2. By calculating the corresponding factorization ofΔ and by taking agcd, this ambiguous form provides the complete prime factorization ofn. This algorithm has these main steps:
Letn be the number to be factored.
To obtain an algorithm for factoring any positive integer, it is necessary to add a few steps to this algorithm such as trial division, and theJacobi sum test.
The algorithm as stated is aprobabilistic algorithm as it makes random choices. Its expected running time is at mostLn[1/2, 1+o(1)].[14]