Skip to content

Instantly share code, notes, and snippets.

@endaaman
Last active October 28, 2017 17:05
Show Gist options
  • Save endaaman/9e17358fbc0a6bdd8d72ac61b1acf94f to your computer and use it in GitHub Desktop.
Save endaaman/9e17358fbc0a6bdd8d72ac61b1acf94f to your computer and use it in GitHub Desktop.
start_time = Time.now
max = 1000000
table = Array.new(max + 1, true)
prime_list = [2]
num = 3
if table[num] == true
prime_list << num
k = num * num
while k <= max
table[k] = false
k += num * 2
end
end
num = 5
while num <= max
if table[num] == true
prime_list << num
k = num * num
while k <= max
table[k] = false
k += num * 2
end
end
num += 2
if table[num] == true
prime_list << num
k = num * num
while k <= max
table[k] = false
k += num * 2
end
end
num += 4
end
p prime_list
puts Time.now - start_time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment