Skip to content

Commit

Permalink
Better sizehint for primes
Browse files Browse the repository at this point in the history
  • Loading branch information
mschauer committed May 12, 2016
1 parent 9c2a233 commit 7f07a50
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/primes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function primes(lo::Int, hi::Int)
lo 3 hi && push!(list, 3)
lo 5 hi && push!(list, 5)
hi < 7 && return list
sizehint!(list, floor(Int, hi / log(hi)))
sizehint!(list, floor(Int, max(hi,0) / log(max(hi,2)) - max(lo,0) / log(max(lo,2))))
sieve = _primesmask(max(7, lo), hi)
lwi = wheel_index(lo - 1)
@inbounds for i = 1:length(sieve) # don't use eachindex here
Expand Down
3 changes: 3 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,9 @@ for T in [Int,BigInt], n = [1:1000;1000000]
@test s[k] == primesmask(k, k)[1]
end
end
let i = rand(1:2^(3*WORD_SIZE÷4))
@test primes(i,i+1476) == filter(isprime, i:i + 1476) == filter(isprime, big(i:i + 1476))
end

@test !isprime(1000000003)
@test !isprime(1000000005)
Expand Down

0 comments on commit 7f07a50

Please sign in to comment.