0
0
RubyProgramBeginner · 2 min read

Ruby Program to Check Prime Number

You can check if a number is prime in Ruby by using a method like def prime?(n); return false if n <= 1; (2..Math.sqrt(n).to_i).none? { |i| n % i == 0 }; end which returns true if n is prime and false otherwise.
📋

Examples

Input2
Output2 is a prime number
Input15
Output15 is not a prime number
Input1
Output1 is not a prime number
🧠

How to Think About It

To check if a number is prime, first exclude numbers less than or equal to 1 because they are not prime. Then, check if any number from 2 up to the square root of the number divides it evenly. If none do, the number is prime; otherwise, it is not.
📐

Algorithm

1
Get the input number n
2
If n is less than or equal to 1, return false (not prime)
3
For each number i from 2 to the square root of n, check if n modulo i equals 0
4
If any i divides n evenly, return false (not prime)
5
If no divisors found, return true (prime)
💻

Code

ruby
def prime?(n)
  return false if n <= 1
  (2..Math.sqrt(n).to_i).none? { |i| n % i == 0 }
end

print "Enter a number: "
num = gets.to_i
if prime?(num)
  puts "#{num} is a prime number"
else
  puts "#{num} is not a prime number"
end
Output
Enter a number: 7 7 is a prime number
🔍

Dry Run

Let's trace the number 7 through the code

1

Check if 7 <= 1

7 is greater than 1, so continue

2

Calculate square root of 7

Math.sqrt(7) ≈ 2.645, so check divisors 2 to 2

3

Check if 7 % 2 == 0

7 % 2 = 1, not zero, so no divisor found

4

No divisors found, return true

7 is prime

i7 % iDivides evenly?
21No
💡

Why This Works

Step 1: Exclude numbers <= 1

Numbers less than or equal to 1 are not prime by definition, so we return false immediately.

Step 2: Check divisors up to square root

A larger divisor would pair with a smaller one already checked, so checking up to the square root is enough.

Step 3: Return true if no divisors found

If no number divides n evenly, n has no factors other than 1 and itself, so it is prime.

🔄

Alternative Approaches

Using a loop with early return
ruby
def prime?(n)
  return false if n <= 1
  (2..Math.sqrt(n).to_i).each do |i|
    return false if n % i == 0
  end
  true
end
This approach uses a loop and returns false immediately when a divisor is found, which can be more efficient for large numbers.
Using Ruby's Prime class
ruby
require 'prime'

print "Enter a number: "
num = gets.to_i
if Prime.prime?(num)
  puts "#{num} is a prime number"
else
  puts "#{num} is not a prime number"
end
This uses Ruby's built-in Prime library for simplicity and reliability but requires loading an extra library.

Complexity: O(√n) time, O(1) space

Time Complexity

The program checks divisors only up to the square root of n, so it runs in O(√n) time.

Space Complexity

The program uses a constant amount of extra space, O(1), as it only stores a few variables.

Which Approach is Fastest?

Using early return in a loop is slightly faster than using none? because it stops checking as soon as a divisor is found. Using Ruby's Prime class is convenient but may have overhead.

ApproachTimeSpaceBest For
Range with none?O(√n)O(1)Simple and readable code
Loop with early returnO(√n)O(1)Faster for large numbers
Ruby Prime classDepends on implementationO(1)Quick use with built-in methods
💡
Check divisors only up to the square root of the number to save time.
⚠️
Checking divisors all the way up to n-1 instead of up to the square root, which is slower.