def next_prime
candidate = next_candidate(@primes[-1])
- while @primes.any? {|i| candidate % i == 0 }
+ limit = Math.sqrt(candidate).floor
+ index = @primes.bsearch_index {|i| i > limit} || @primes.length
+ while @primes[0..index].any? {|i| candidate % i == 0 }
candidate = next_candidate(candidate)
end
candidate
end
def include?(n)
- while n >= @primes.max
+ while n >= @primes.last
add_next_prime!
end
- @primes.include?(n)
+ # @primes.include?(n)
+ !!(@primes.bsearch {|i| n - i})
end
end