Last active
October 28, 2017 17:05
-
-
Save endaaman/9e17358fbc0a6bdd8d72ac61b1acf94f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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