Created
May 4, 2014 07:25
-
-
Save ttanimichi/c7c0cdb2951270c21380 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
MAX = 100 | |
target = Array(2..MAX) | |
prime_numbers = [] | |
LIMIT = Math.sqrt(MAX) | |
until target.first > LIMIT | |
sieve = target.shift | |
prime_numbers << sieve | |
target.delete_if {|n| (n % sieve).zero? } | |
end | |
p prime_numbers + target #=> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment