193
193
194
194
"""
195
195
isprime(x::BigInt, [reps = 25]) -> Bool
196
-
197
196
Probabilistic primality test. Returns `true` if `x` is prime with high probability (pseudoprime);
198
197
and `false` if `x` is composite (not prime). The false positive rate is about `0.25^reps`.
199
198
`reps = 25` is considered safe for cryptographic applications (Knuth, Seminumerical Algorithms).
200
-
201
199
```julia
202
200
julia> isprime(big(3))
203
201
true
@@ -307,11 +305,11 @@ function iterate(f::FactorIterator{T}, state=(f.n, T(3))) where T
307
305
num_p += 1
308
306
end
309
307
return (p, num_p), (n, p)
310
- elseif isprime (n)
308
+ elseif p == 3 && isprime (n)
311
309
return (n, 1 ), (T (1 ), n)
312
310
end
313
311
for p in p: 2 : N_SMALL_FACTORS
314
- isprime (p) || continue
312
+ _min_factor (p) == p || continue
315
313
num_p = 0
316
314
while true
317
315
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
320
318
n = q
321
319
end
322
320
if num_p > 0
323
- return (p, num_p), (n, p)
321
+ return (p, num_p), (n, p+ 2 )
324
322
end
325
323
p* p > n && break
326
324
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
328
329
should_widen = T <: BigInt || widemul (n - 1 , n - 1 ) ≤ typemax (n)
329
330
p = should_widen ? pollardfactor (n) : pollardfactor (widen (n))
330
331
num_p = 0
0 commit comments