Factoring

Factoring

Splitting a number into primes: trial division for the small factors, Pollard's rho for the medium ones, and the p − 1 method when a factor happens to have a smooth neighbour. Every prime in the answer says whether it is proven.

The whole factorisation

import { factor, formatFactors } from "@ratmath/numtheory";

formatFactors(factor(600851475143n));   // "71 · 839 · 1471 · 6857"
formatFactors(factor(2n ** 64n + 1n));  // "274177 · 67280421310721": Landry's 1880 split of F₆
formatFactors(factor(10n ** 20n + 1n)); // "73 · 137 · 1676321 · 5964848081"
factor(2n ** 67n - 1n);
// [{ prime: 193707721n, exponent: 1, proven: true },
//  { prime: 761838257287n, exponent: 1, proven: true }]

factor divides out the primes below 10510^5, then splits what is left: a probable prime goes into the answer, a perfect square is split into its roots, anything else goes to Pollard's rho, then to p−1p - 1, then to the elliptic curve method. Factors below 2642^{64} are proven by Baillie–PSW; a larger one is proven by a certificate, and proven: false says when that could not be found.

Pollard's rho

Iterate x↦x2+c(modn)x \mapsto x^2 + c \pmod n. Modulo an unknown prime p∣np \mid n the sequence lives in a set of size pp, so it must repeat, and by the birthday paradox it does so after about p\sqrt{p} steps: its picture is the Greek letter ρ\rho, a tail and a loop. Once xi≡xj(modp)x_i \equiv x_j \pmod p, gcd⁡(xi−xj,n)\gcd(x_i - x_j, n) is a multiple of pp. Brent's version finds the loop by comparing against a saved value at powers of two, and multiplies many differences together before taking one gcd. The cost is about p\sqrt p multiplications: a 12-digit factor takes around a million.

pollardRho(1000000007n * 998244353n);   // 998244353n
pollardPm1(1000000007n * 1000000009n);  // 1000000009n
pollardPm1(2n ** 67n - 1n);             // null: see below

Pollard's p − 1

If p−1p - 1 has only prime factors below a bound BB, then p−1p - 1 divides M=∏q≤Bq⌊log⁡qB⌋M = \prod_{q \le B} q^{\lfloor \log_q B \rfloor}, and by Fermat aM≡1(modp)a^M \equiv 1 \pmod p, so gcd⁡(aM−1,n)\gcd(a^M - 1, n) picks pp out. It finds 1000000009 in 1000000007⋅10000000091000000007 \cdot 1000000009 at once, because 1000000008=23⋅32⋅7⋅1092⋅1671000000008 = 2^3 \cdot 3^2 \cdot 7 \cdot 109^2 \cdot 167 is smooth while 1000000006=2⋅5000000031000000006 = 2 \cdot 500000003 is not. It can also be too lucky: for Cole's number 267−12^{67} - 1 both p−1p - 1s are smooth (193707720=23⋅33⋅5⋅67⋅2677193707720 = 2^3 \cdot 3^3 \cdot 5 \cdot 67 \cdot 2677), both factors drop out together, the gcd is nn itself, and the method reports nothing.

Beyond rho

Rho slows down when the smallest prime factor passes about 12 digits. The seventh Fermat number 2128+12^{128} + 1 is the product of two primes of 17 and 22 digits: rho would need hundreds of millions of steps, and p−1p - 1 fails on it. So after a short run of rho and p−1p - 1, factor goes on to the elliptic curve method, which is to p−1p - 1 what a family of groups is to one group, and which finds that 17-digit factor in a fraction of a second. Factors of up to about 25 digits are within reach; beyond that factor gives up with the reason rather than running for hours.

Try itEdit the code and run it with @ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs