Suppose we are trying to factor the composite numbern. We choose a bound B, and identify the factor base, the set of all primes less than or equal toB. Next, we search for positive integersz such that both z and z+n are B-smooth — i.e. all of their prime factors are in P. We can therefore write, for suitable exponents, and likewise, for suitable, we have But and are congruent modulo, and so each such integer z that we find yields a multiplicative relation among the elements of P, i.e. When we have generated enough of these relations, we can use the methods of linear algebra to multiply together these various relations in such a way that the exponents of the primes are all even. This will give us a congruence of squares of the form a2≡b2, which can be turned into a factorization of n = gcd×gcd. This factorization might turn out to be trivial, in which case we have to try again with a different combination of relations; but with luck we will get a nontrivial pair of factors of n, and the algorithm will terminate.
Example
We will factor the integer n = 187 using the rational sieve. We'll arbitrarily try the value B=7, giving the factor base P = . The first step is to test n for divisibility by each of the members of P; clearly if n is divisible by one of these primes, then we are finished already. However, 187 is not divisible by 2, 3, 5, or 7. Next, we search for suitable values of z; the first few are 2, 5, 9, and 56. The four suitable values of z give four multiplicative relations :
21305070 = 2 ≡ 189 = 20335071.............
20305170 = 5 ≡ 192 = 26315070.............
20325070 = 9 ≡ 196 = 22305072.............
23305071 = 56 ≡ 243 = 20355070.............
There are now several essentially different ways to combine these and end up with even exponents. For example,
×: After multiplying these and canceling out the common factor of 7, this reduces to 24 ≡ 38, or 42 ≡ 812. The resulting factorization is 187 = gcd × gcd = 11×17.
Alternatively, equation is in the proper form already:
: This says 32 ≡ 142, which gives the factorization 187 = gcd × gcd = 11×17.
Limitations of the algorithm
The rational sieve, like the general number field sieve, cannot factor numbers of the form pm, where p is a prime and m is an integer. This is not a huge problem, though—such numbers are statistically rare, and moreover there is a simple and fast process to check whether a given number is of this form. Probably the most elegant method is to check whether holds for any 1 < b < log using an integer version of Newton's method for the root extraction. The biggest problem is finding a sufficient number of z such that both z and z+n are B-smooth. For any given B, the proportion of numbers that are B-smooth decreases rapidly with the size of the number. So if n is large, it will be difficult or impossible to find enough z for the algorithm to work. The advantage of the general number field sieve is that one need only search for smooth numbers of order n1/d for some positive integerd, rather than of order n as required here.