Skip to content

Commit dc67611

Browse files
authored
fix eachfactor type stability (#152)
* fix `eachfactor` type stability fixes #151. (test incoming) * add test
1 parent 2eb1345 commit dc67611

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/Primes.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ function iterate(f::FactorIterator{T}, state=(f.n, T(3))) where T
357357
tz = trailing_zeros(n)
358358
tz>0 && return (T(2), Int(tz)), (n >> tz, p)
359359
if n <= N_SMALL_FACTORS
360-
p = _min_factor(n)
360+
p = T(_min_factor(n))
361361
num_p = 1
362362
while true
363363
n = div(n, p)
@@ -369,7 +369,7 @@ function iterate(f::FactorIterator{T}, state=(f.n, T(3))) where T
369369
elseif p == 3 && isprime(n)
370370
return (n, 1), (T(1), n)
371371
end
372-
for p in p:2:N_SMALL_FACTORS
372+
for p in p:T(2):T(N_SMALL_FACTORS)
373373
_min_factor(p) == p || continue
374374
num_p = 0
375375
while true

test/runtests.jl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,16 @@ end
247247
@test all([issorted(collect(factor(rand(Int)))) for x in 1:100])
248248

249249
# test eachfactor iteration
250-
@test iterate(eachfactor(36)) == ((2, 2), (9, 3))
251-
@test iterate(eachfactor(7^2*5^3)) == ((5, 3), (49, 5))
252-
@test iterate(eachfactor(257)) == ((257, 1), (1, 257))
253-
@test iterate(eachfactor(nextprime(2^16))) == ((65537, 1), (1, 65537))
254-
250+
for T in (Int32, Int64, BigInt)
251+
@test iterate(eachfactor(T(36))) == ((T(2), 2), T.((9, 3)))
252+
@test iterate(eachfactor(T(7^2*5^3))) == ((T(5), 3), T.((49, 5)))
253+
@test iterate(eachfactor(T(257))) == ((T(257), 1), T.((1, 257)))
254+
@test iterate(eachfactor(T(nextprime(2^16)))) == ((T(65537), 1), T.((1, 65537)))
255+
for (p,e) in eachfactor(T(901800900))
256+
@test (p,e) isa Tuple{T, Int}
257+
end
258+
end
259+
255260
# Lucas-Lehmer
256261
@test !ismersenneprime(2047)
257262
@test ismersenneprime(8191)
@@ -341,7 +346,7 @@ divisors_brute_force(n) = [d for d in one(n):n if iszero(n % d)]
341346

342347
for n in 2:1000
343348
ds = divisors(T(n))
344-
@test ds == divisors(-T(n))
349+
@test ds == divisors(-T(n))
345350
@test sort!(ds) == divisors_brute_force(T(n))
346351
end
347352
end

0 commit comments

Comments
 (0)