Skip to content

Instantly share code, notes, and snippets.

@hiroism007
Created April 20, 2013 11:45
Show Gist options
  • Save hiroism007/5425721 to your computer and use it in GitHub Desktop.
Save hiroism007/5425721 to your computer and use it in GitHub Desktop.
エラトステネスのふるい
puts "素数リストを作成します。数えたい最大の数を入れてください\n"
max_number = gets.to_i
primes = {}
for i in 2..max_number do primes[i] =true end
for i in 2..max_number do
(2*i).step(max_number,i){|n| primes[n] = false } if primes[i]==true
end
p primes.delete_if{|k,v| v == false}.keys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment