Skip to content

Instantly share code, notes, and snippets.

@mwunsch
Created November 4, 2013 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwunsch/7306524 to your computer and use it in GitHub Desktop.
Save mwunsch/7306524 to your computer and use it in GitHub Desktop.
def sieve(n)
integers = 2..n
not_primes = (2..Math.sqrt(n).ceil).reduce([]) do |marked, p|
(p**2).step(n, p) {|j| marked << j }
marked
end
integers.to_a - not_primes
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment