Processing is controlled by the factors function, which we will look at in pieces; you can see the entire function on the next page. We begin with the internal definition of the factor? function. The rho, p-1 and elliptic curve functions either return a divisor or #f to indicate failure, and the divisor is not necessarily prime. Factor? handles the various return values. If the factorization fails, factor? returns #f to its caller. If the divisor is prime, factor? updates the current result (stored in the variables n and facts), exits if the remaining co-factor is prime, or returns #t so the factorization can continue. If the divisor is not prime, factor? calls factors recursively to factor the non-prime divisor, exits with failure if the non-prime divisor can’t be factored, exits with success if the factorization of the non-prime divisor completes the overall factorization, or returns #t so the overall factorization can continue.

The body of the factors function first checks if the input number is prime, then performs trial division, rho, p-1 and elliptic curve factorization according to the input parameters, and finally reports failure if the factorization is incomplete. The exit function, provided by call-with-current-continuation, exits as soon as the factorization is complete.

The key to factors is the list of parameters at the top of the function, which may be tuned as desired; the given list is only a starting point. We perform trial divisions to 100000, then five trials of Pollard’s rho method with 100000 iterations each (trials that find a factor don’t count against the limit of five), then Pollard’s p-1 to a first-stage limit of 500000, then two-stage elliptic curve factorization with up to 100 curves, with the bounds increasing for each curve. There is nothing particularly magical about these parameters, which were determined by watching the progress of multiple factorizations, sometimes trying alternate parameters to see the effect.

Pollard’s rho algorithm is the only one that changed from our original implementation. The original kept working until it found a factor or fell into a cycle, but our new implementation continues until it reaches an iteration limit.

We are lucky to have an example factorization that uses all five factorization methods — trial division, Pollard rho, Pollard p-1, elliptic curve first stage, and elliptic curve second stage — and even finds a non-prime factor that must be resolved recursively; it takes about twenty minutes on a recent-vintage personal computer to factor the 180-digit number consisting of the digit 1 repeated 180 times (verbose? was set to #t and the random-number seed was set to 87654321 immediately before this computation):