Skip to content

Instantly share code, notes, and snippets.

@astronomy88
Last active August 29, 2015 13:57
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 astronomy88/9635711 to your computer and use it in GitHub Desktop.
Save astronomy88/9635711 to your computer and use it in GitHub Desktop.
end_value = 120
#-- Without using mod
b = Array(2..end_value)
primes = Array(2..end_value)
limit = Math.sqrt end_value
b.each do |z|
break if z >= limit
primes.each do |i|
break if (z*i) > b.last
next if i == 0
primes[(z*i)-2] = 0 if primes[(z*i)-2] == (z * i)
end
end
primes.delete_if { |j| j == 0 }
puts "Sieve of erastothenes: #{primes}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment