Skip to content

Instantly share code, notes, and snippets.

@damjes
Last active June 24, 2021 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damjes/ea17120e00114c1daa71f2dee9d079fc to your computer and use it in GitHub Desktop.
Save damjes/ea17120e00114c1daa71f2dee9d079fc to your computer and use it in GitHub Desktop.
Sundaram sieve
# 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