From 396713a319ac42efb6c568ca95782cbb8df93316 Mon Sep 17 00:00:00 2001 From: mschauer Date: Fri, 27 May 2016 20:14:28 +0200 Subject: [PATCH] Better sizehint for ``primes`` --- src/Primes.jl | 2 +- test/runtests.jl | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Primes.jl b/src/Primes.jl index 8625b08..d453021 100644 --- a/src/Primes.jl +++ b/src/Primes.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 3fbf83f..ae0f7c5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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)