Skip to content

Instantly share code, notes, and snippets.

@ochim
Created May 22, 2016 12:19
Show Gist options
  • Save ochim/99c93dc9201b8d3a7baba874a959865e to your computer and use it in GitHub Desktop.
Save ochim/99c93dc9201b8d3a7baba874a959865e to your computer and use it in GitHub Desktop.
素数判定
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