Skip to content

Instantly share code, notes, and snippets.

@hzm-s
Created April 24, 2016 08:26
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 hzm-s/8ae04c6e95316323e3e13ece3d2f19bd to your computer and use it in GitHub Desktop.
Save hzm-s/8ae04c6e95316323e3e13ece3d2f19bd to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
def compute(number)
sieve((2..number).to_a, [], Math.sqrt(number))
end
def sieve(list, primes, stopper)
return primes + list if list.first >= stopper
primes << list.shift
list.reject! { |n| n % primes.last == 0 }
sieve(list, primes, stopper)
end
if ($0 == __FILE__)
puts compute(ARGV.first.to_i).join(', ')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment