Skip to content

Instantly share code, notes, and snippets.

@ttanimichi
Created May 4, 2014 07:25
Show Gist options
  • Save ttanimichi/c7c0cdb2951270c21380 to your computer and use it in GitHub Desktop.
Save ttanimichi/c7c0cdb2951270c21380 to your computer and use it in GitHub Desktop.
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