-
-
Save opamp/1192652 to your computer and use it in GitHub Desktop.
ProjectEuler7
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
#!/usr/bin/env ruby | |
#-*- encoding:utf-8 -*- | |
# * へっぽこコード | |
def isPnum?(a) #素数か判定する | |
sa = (Math::sqrt(a)).truncate | |
return true if a == 2 or a == 3 | |
return false if a < 2 | |
2.upto(sa) do |x| | |
if a % x == 0 then | |
return false | |
end | |
end | |
return true | |
end | |
def pnum(n) #nまでの素数を返すイテレータ (Problem7を解くのには必要ないけど) | |
i = 2 | |
while(i <= n) | |
if isPnum?(i) == true then | |
yield i | |
end | |
i+=1 | |
end | |
end | |
p,i = 0,0 | |
loop do #今回の処理 | |
if isPnum?(i) == true | |
p+=1 | |
if p == 10001 then | |
puts "#{i}" | |
break | |
end | |
end | |
i+=1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment