Skip to content

Instantly share code, notes, and snippets.

@opamp
Created September 4, 2011 10:42
Show Gist options
  • Save opamp/1192652 to your computer and use it in GitHub Desktop.
Save opamp/1192652 to your computer and use it in GitHub Desktop.
ProjectEuler7
#!/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