Skip to content

Commit ff78c81

Browse files
authored
use increment! for faster updating
1 parent 5255435 commit ff78c81

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/Primes.jl

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

255255
local p::T
256256
for p in PRIMES
257+
num_p = 0
257258
while true
258259
q, r = divrem(n, T(p)) # T(p) so julia <1.9 uses fast divrem for `BigInt`
259-
r == 0 || break
260-
h[p] = get(h, p, 0) + 1
260+
if r == 0
261+
increment!(h, num_p, p) # h[p] += num_p (about 2x faster, but the speed only matters for small numbers)
262+
break
263+
end
264+
num_p += 1
261265
n = q
262266
end
263267
p*p > n && break

0 commit comments

Comments
 (0)