@@ -260,30 +260,28 @@ isprime(n::Int128) = n < 2 ? false :
260
260
function factor! (n:: T , h:: AbstractDict{K,Int} ) where {T<: Integer ,K<: Integer }
261
261
# check for special cases
262
262
if n < 0
263
- h[- 1 ] = 1
263
+ h[- 1 ] = 1 - h[ - 1 ]
264
264
if isa (n, BitSigned) && n == typemin (T)
265
- h[2 ] = 8 * sizeof (T) - 1
265
+ h[2 ] + = 8 * sizeof (T) - 1
266
266
return h
267
267
else
268
268
return factor! (checked_neg (n), h)
269
269
end
270
270
elseif n == 0
271
- h[n] = 1
271
+ h[n] = 0
272
272
return h
273
273
elseif n <= length (MIN_FACTOR)
274
274
while true
275
275
n == 1 && return h
276
276
if MIN_FACTOR[n]== 1
277
- increment! (h, 1 , n)
278
- return h
277
+ return increment! (h, 1 , n)
279
278
else
280
279
increment! (h, 1 , MIN_FACTOR[n])
281
280
n = div (n, MIN_FACTOR[n])
282
281
end
283
282
end
284
283
elseif isprime (n)
285
- h[n] = 1
286
- return h
284
+ return increment! (h, 1 , n)
287
285
end
288
286
local p:: T
289
287
for p in 2 : length (MIN_FACTOR)
@@ -296,11 +294,15 @@ function factor!(n::T, h::AbstractDict{K,Int}) where {T<:Integer,K<:Integer}
296
294
n = q
297
295
end
298
296
# h[p] += num_p (about 2x faster, but the speed only matters for small numbers)
299
- num_p > 0 && increment! (h, num_p, p)
297
+ if num_p > 0
298
+ increment! (h, num_p, p)
299
+ # if n is small, then recursing will hit the fast path.
300
+ n < length (MIN_FACTOR) && return factor! (n, h)
301
+ end
300
302
p* p > n && break
301
303
end
302
304
n == 1 && return h
303
- isprime (n) && (h[n] = 1 ; return h )
305
+ isprime (n) && return increment! (h, 1 , n )
304
306
T <: BigInt || widemul (n - 1 , n - 1 ) ≤ typemax (n) ? pollardfactors! (n, h) : pollardfactors! (widen (n), h)
305
307
end
306
308
0 commit comments