Skip to content

Instantly share code, notes, and snippets.

@durrellchamorro
Last active November 21, 2015 03:01
Show Gist options
  • Save durrellchamorro/4dc5f217056326e25319 to your computer and use it in GitHub Desktop.
Save durrellchamorro/4dc5f217056326e25319 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
class Sieve
def initialize(limit)
@number_list = (2..limit).to_a
end
def primes
prime_numbers = []
until @number_list.empty?
prime_numbers << @number_list.first
@number_list -= multiples_of @number_list.first
end
prime_numbers
end
private
def multiples_of(first_number_in_list)
@number_list.select { |number| number % first_number_in_list == 0 }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment