Last active
February 10, 2017 18:55
-
-
Save zeitan/b782176d6348afe47ec28bc40e9f7e68 to your computer and use it in GitHub Desktop.
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
# https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes | |
class Sieve | |
def initialize(number) | |
@limit = number | |
end | |
def primes | |
prime_numbers = [] | |
return prime_numbers if @limit == 1 | |
data = (2..@limit).to_a | |
begin | |
prime_numbers << data.shift | |
return prime_numbers unless data.length.nonzero? | |
data.select!{ |element| (element % prime_numbers.last).nonzero? } | |
end while true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment