LoginSignup
0
0

【Ruby】素数を判定するプログラム

Last updated at Posted at 2020-10-20

数字を入力して素数ならtrue、素数じゃなかったらfalseを返す。

while str = STDIN.gets
  break if str.chomp == "exit"
n=str.to_i
count=0
for i in 1..n+1 do
    d=n%i
    if d==0 then
        count+=1
    end
end

if count==2 then
    print("true")
else
    print("false")
end
puts "\n"
end

Rubyのお役立ち情報

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0