# Integer factorization in 64 bit

Remco Bloemen

2014-02-27, last updated 2014-03-04

For a residue number system I am trying to implement I need to factor 64 bit integers n. They have the special for that n+1 is a prime number, so they are all divisible by two and they tend to have other small factors. Here are a few representative examples:

18446744073709551556=2211137547559447261764118446744073709551532=2243671938093831024719718446744073709551520=25526634329408579071918446744073709551436=2241101405161994434765118446744073709551426=2323133672058505141677 \begin{aligned} 18446744073709551556 &= 2^2 \cdot 11 \cdot 137 \cdot 547 \cdot 5594472617641 \\ 18446744073709551532 &= 2^2 \cdot 43 \cdot 67 \cdot 193 \cdot 809383 \cdot 10247197 \\ 18446744073709551520 &= 2^5 \cdot 5 \cdot 2663 \cdot 43294085790719 \\ 18446744073709551436 &= 2^2 \cdot 41 \cdot 101 \cdot 4051 \cdot 6199 \cdot 44347651 \\ 18446744073709551426 &= 2 \cdot 3 \cdot 23 \cdot 133672058505141677 \end{aligned}

Trial division

Trial division is the simplest method, simply start dividing out numbers from one to n\sqrt{n}. Since you will factor out the primes before any of their composites the resulting list only contains prime factors. I also tested a version which used a list of small primes first, before trying all numbers, but the extra memory bandwidth was worse than the speedup from having to try less numbers. Here is the source:

Wheel factorisation

In the previous example I took the two out as a special case and skipped all the odd integers. A generalisation of this trick is wheel factorisation, where a few small primes are taken out and a skip list is produced of numbers that must be multiples of those small primes. The end result is a list of numbers that contains more primes (without missing any!) than plainly listing all odd integers.

There is a trade off between memory usage by the skip list and small primes list and the percentage of composites that are skipped. As often with these memory trade offs, its best to choose the size such that it fits snugly in the cpu cache. Now that I think about it, I could probably take 32 bit numbers for the primes and skip list, and fit twice the amount in the cpu cache.

The small primes are produced using the Sieve of Erastosthenes I implemented earlier. This implementation creates the largest possible wheel for a given number of small primes, which can then be used to factor integers.

Brent-Pollard rho factorisation

This is John Pollard’s rho factorisation method (see wikipedia and planet math. It operates by iterating a random function f modulo n. This iteration must become cyclic sometime since there are only a finite amount numbers modulo n. The congruence relations resulting from this cycle give us clues about factors of n.

Richard Brent has made quite a few improvements to this algorithm. The original algorithm compares xix_i with x2ix_{2i} to detect a cycle, but I implemented Brent’s improvement, which compares xix_i with xjx_j, where jj is the first power of two below ii. Brent also suggested that one can multiply a few differences |xixj|\left\vert x_i - x_j \right\vert modulo nn and compute the gcd of the product and nn once in a while. I did not implement this last suggestion, but I did measure that >90% of the time is spent computing gcd’s, so this would be an important improvement. I also did not implement Montogomery reduced multiplication, which may speed up the multiplication.

This algorithm will run indefinitely for prime numbers, so these are checked for on start. There are also some special cases where the cycle may be very long or where it will not result in a proper factor, so after a while the algorithm terminates and tries again with a different random function. I have not thought hard about the best time to terminate and try again, so this might be sub-optimal.

Benchmark and conclusion

The algorithms are compared by factoring p-1 for the thousand largest 64 bit primes:

Algorithm Running time
Trial division 959 s
Wheel factorisation 544 s
Brent-Pollard 3 s

Clearly the Brent-Pollard method is the way to go, I should implement the optimisations I suggested and think more thoroughly about the loop termination.