Skip to content

Commit c1378d0

Browse files
authored
simpler and slightly faster factorization
`divrem` is faster than separate `div` and `rem`
1 parent 88d6227 commit c1378d0

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/Primes.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,14 @@ function factor!(n::T, h::AbstractDict{K,Int}) where {T<:Integer,K<:Integer}
256256
nsqrt = isqrt(n)
257257
for p in PRIMES
258258
p > nsqrt && break
259-
if n % p == 0
259+
while true
260+
q, r == divrem(n, T(p)) # T(p) so julia <1.9 uses fast divrem for `BigInt`
261+
r == 0 || break
260262
h[p] = get(h, p, 0) + 1
261-
n = div(n, p)
262-
while n % p == 0
263-
h[p] = get(h, p, 0) + 1
264-
n = div(n, p)
265-
end
266-
n == 1 && return h
267-
nsqrt = isqrt(n)
263+
n = q
268264
end
265+
n == 1 && return h
266+
nsqrt = isqrt(n)
269267
end
270268
isprime(n) && (h[n]=1; return h)
271269
T <: BigInt || widemul(n - 1, n - 1) typemax(n) ? pollardfactors!(n, h) : pollardfactors!(widen(n), h)

0 commit comments

Comments
 (0)