JuliaMath / Primes.jl

Prime numbers in Julia
Other
99 stars 32 forks source link

isprime(n) allocates #125

Closed stevengj closed 1 year ago

stevengj commented 1 year ago

See this discourse comment.

It seems like this could be fixed with a little refactoring. Instead of having witnesses(n) return a variably sized tuple (which is not type stable), it would be better to inline this into isprime and do something like:

return n < 4294967296  ? checkwitnesses(n, _witnesses(UInt64(n))) :
        n < 2152302898747   ? checkwitnesses(n, (2, 3, 5, 7, 11)) :
        n < 3474749660383   ? checkwitnesses(n, (2, 3, 5, 7, 11, 13)):
                              checkwitnesses(n, (2, 325, 9375, 28178, 450775, 9780504, 1795265022))

where checkwitnesses(n, witnesses) is simply this loop refactored into a function.

oscardssmith commented 1 year ago

it can. I've done that refactoring in a local branch and never bothered to pr it.

oscardssmith commented 1 year ago

this is fixed