Last active
November 21, 2015 03:01
-
-
Save durrellchamorro/4dc5f217056326e25319 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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