Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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