Skip to content

Commit 4dd4888

Browse files
author
oscarddssmith
committed
clean up
1 parent 4098c5c commit 4dd4888

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/Primes.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,9 @@ end
193193

194194
"""
195195
isprime(x::BigInt, [reps = 25]) -> Bool
196-
197196
Probabilistic primality test. Returns `true` if `x` is prime with high probability (pseudoprime);
198197
and `false` if `x` is composite (not prime). The false positive rate is about `0.25^reps`.
199198
`reps = 25` is considered safe for cryptographic applications (Knuth, Seminumerical Algorithms).
200-
201199
```julia
202200
julia> isprime(big(3))
203201
true
@@ -307,11 +305,11 @@ function iterate(f::FactorIterator{T}, state=(f.n, T(3))) where T
307305
num_p += 1
308306
end
309307
return (p, num_p), (n, p)
310-
elseif isprime(n)
308+
elseif p == 3 && isprime(n)
311309
return (n, 1), (T(1), n)
312310
end
313311
for p in p:2:N_SMALL_FACTORS
314-
isprime(p) || continue
312+
_min_factor(p) == p || continue
315313
num_p = 0
316314
while true
317315
q, r = divrem(n, T(p)) # T(p) so julia <1.9 uses fast divrem for `BigInt`
@@ -320,11 +318,14 @@ function iterate(f::FactorIterator{T}, state=(f.n, T(3))) where T
320318
n = q
321319
end
322320
if num_p > 0
323-
return (p, num_p), (n, p)
321+
return (p, num_p), (n, p+2)
324322
end
325323
p*p > n && break
326324
end
327-
isprime(n) && (n, 1), (T(1), n)
325+
# if n < 2^32, then if it wasn't prime, we would have found the factors by trial division
326+
if n <= 2^32 || isprime(n)
327+
return (n, 1), (T(1), n)
328+
end
328329
should_widen = T <: BigInt || widemul(n - 1, n - 1) typemax(n)
329330
p = should_widen ? pollardfactor(n) : pollardfactor(widen(n))
330331
num_p = 0

0 commit comments

Comments
 (0)