Skip to content

Instantly share code, notes, and snippets.

@keroxp
Created June 27, 2012 12:24
Show Gist options
  • Save keroxp/3003723 to your computer and use it in GitHub Desktop.
Save keroxp/3003723 to your computer and use it in GitHub Desktop.
素数を出力するプログラム
class Prime
def self.calc
primes = [2]
puts 2
i = 3
loop do
max = Math.sqrt(i).floor
primes.each do |p|
if p > max then
yield i
primes << i
break
end
if i % p == 0 then
break
end
end
i += 1
end
end
end
if $0 == __FILE__
Prime.calc { |p|
puts p
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment