Skip to content

new-factor is excruciatingly wrong

The factors function in l/new-factor.lisp factors a number fine in the first try. On subsequent tries, it fails totally. Example:

> (factors 10000000001)
(101 3541 27961)

; 1.94e6 call 4.20e6 cons 3.28e6 eval 0.244 s 1.34e7 eval/s
> (factors 10000000001)
(10000000001)

; 7.33e6 call 1.58e7 cons 1.23e7 eval 0.494 s 2.49e7 eval/s

The function optimises for subsequent runs by memorising the consecutive prime numbers it has found in earlier attempts, which makes for a significant difference between the first and the second run. That must be the area where that bug is.