Skip to content

Commit 751eac7

Browse files
authored
Merge pull request #114 from JuliaMath/oscardssmith-faster-factor
simpler and slightly faster factorization
2 parents 88d6227 + 1517cc8 commit 751eac7

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

src/Primes.jl

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,20 +253,16 @@ function factor!(n::T, h::AbstractDict{K,Int}) where {T<:Integer,K<:Integer}
253253
end
254254

255255
local p::T
256-
nsqrt = isqrt(n)
257256
for p in PRIMES
258-
p > nsqrt && break
259-
if n % p == 0
257+
while true
258+
q, r = divrem(n, T(p)) # T(p) so julia <1.9 uses fast divrem for `BigInt`
259+
r == 0 || break
260260
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)
261+
n = q
268262
end
263+
p*p > n && break
269264
end
265+
n == 1 && return h
270266
isprime(n) && (h[n]=1; return h)
271267
T <: BigInt || widemul(n - 1, n - 1) typemax(n) ? pollardfactors!(n, h) : pollardfactors!(widen(n), h)
272268
end

0 commit comments

Comments
 (0)