Skip to content

Commit

Permalink
Merge pull request #11 from JuliaMath/sizehint
Browse files Browse the repository at this point in the history
Better sizehint for ``primes``
  • Loading branch information
ararslan committed May 27, 2016
2 parents 0f91dd2 + 396713a commit 6467a59
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Primes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,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, 5 + floor(Int, hi / (log(hi) - 1.12) - lo / (log(max(lo,2)) - 1.12*(lo > 7))) ) # http://projecteuclid.org/euclid.rmjm/1181070157
sieve = _primesmask(max(7, lo), hi)
lwi = wheel_index(lo - 1)
@inbounds for i = 1:length(sieve) # don't use eachindex here
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ for n = 1:100
@test n == m
end

let i = rand(1:2^(3*min(WORD_SIZE,64)÷4))
@test primes(i,i+300) == filter(isprime, i:i + 300) == filter(isprime, big(i:i + 300))
end


@test isprime(BigInt(1000000007))
@test isprime(BigInt(1000000007), 1)
Expand Down

0 comments on commit 6467a59

Please sign in to comment.