Created
May 22, 2016 12:19
-
-
Save ochim/99c93dc9201b8d3a7baba874a959865e to your computer and use it in GitHub Desktop.
素数判定
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
def isPrime(i) | |
# 1は素数ではない | |
return false if i == 1 | |
# 2は素数 | |
return true if i == 2 | |
primeNumbers = [2] | |
judge = true | |
# 入力値より小さい素数を求めながら、入力値が素数であるか判定する | |
(3..i).each do |j| | |
judge = true | |
primeNumbers.each do |number| | |
if j % number == 0 | |
judge = false; | |
break; | |
end | |
end | |
primeNumbers.push(j) if judge | |
#p primeNumbers | |
end | |
return judge | |
end | |
input = gets.to_i | |
puts isPrime(input) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment