Skip to content

Instantly share code, notes, and snippets.

@leeacto
Last active December 19, 2015 04:19
Show Gist options
  • Save leeacto/5896369 to your computer and use it in GitHub Desktop.
Save leeacto/5896369 to your computer and use it in GitHub Desktop.
def sieve(num)
first_arr = Array.new(num) { |i| i }
2.times { first_arr.delete_at(0) }
primes = []
while true
break if first_arr[0]**2 >= num
first_arr.delete_if { |n| n % first_arr[0] == 0 && first_arr[0] != n }
primes << first_arr.slice!(0)
end
primes += first_arr
primes.count
end
st = Time.new
puts sieve(97531)
en = Time.new - st
puts en
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment