Last active
June 24, 2021 19:20
-
-
Save damjes/ea17120e00114c1daa71f2dee9d079fc to your computer and use it in GitHub Desktop.
Sundaram sieve
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
# IT REALLY WORKS! CHECK IT AT https://bit.ly/3d9iWIU ! | |
def primes(limit) | |
limit /= 2 | |
t = [true] * limit | |
t[0] = false | |
i = 0 | |
k = 1 | |
while i < limit | |
i += k*2 + 2 | |
j = i | |
k += 2 | |
while j < limit | |
t[j] = false | |
j += k | |
end | |
end | |
primes = [2] | |
t.each_with_index do |prime, index| | |
primes << 2*index + 1 if prime | |
end | |
primes | |
end | |
puts primes(100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment