[Ruby] A program that determines prime numbers

Enter a number and return true if it is a prime number, false if it is not a prime number.

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

October 20, 2020

Recommended Posts

[Ruby] A program that determines prime numbers
[Ruby] A program / concept that combines each_with_index and search
[ruby] Creating a program that responds only to specific conditions
Ruby: I made a FizzBuzz program!
A program that calculates factorials from 2 to 100
A program that determines whether the entered integer is close to an integer
Ruby payroll program
Judge prime numbers
I wrote a prime factorization program in Java
A ruby ​​script that creates an rsa private key that can be used with OpenSSL from any two prime numbers
[Ruby] I want to make a program that displays today's day of the week!
Calculate the difference between numbers in a Ruby array
A reference book that my brother had, had, had Ruby, Java
How to launch another command in a Ruby program
Safe numbers (ruby edition)
Ruby score calculation program
A program (Java) that outputs the sum of odd and even numbers in an array
Created a menu program
Ruby Learning # 9 Math & Numbers
Ruby Numeric Reference Program
Determine that the value is a multiple of 〇 in Ruby
A program that counts the number of words in a List