Skip to content

Instantly share code, notes, and snippets.

@astronomy88 astronomy88/sieve_nomod.rb
Last active Aug 29, 2015

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)
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
You can’t perform that action at this time.