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 , 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 , then to the elliptic curve method. Factors below 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 . Modulo an unknown prime the sequence lives in a set of size , so it must repeat, and by the birthday paradox it does so after about steps: its picture is the Greek letter , a tail and a loop. Once , is a multiple of . 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 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 has only prime factors below a bound , then divides , and by Fermat , so picks out. It finds 1000000009 in at once, because is smooth while is not. It can also be too lucky: for Cole's number both s are smooth (), both factors drop out together, the gcd is itself, and the method reports nothing.
Beyond rho
Rho slows down when the smallest prime factor passes about 12 digits. The seventh Fermat number is the product of two primes of 17 and 22 digits: rho would need hundreds of millions of steps, and fails on it. So after a short run of rho and , factor goes on to the elliptic curve method, which is to 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.
@ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.