Open In App

Ruby Integer prime? function with example

Last Updated : 03 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The prime? function in Ruby returns a boolean value. It returns true if the number is prime, else it returns false. It requires the ‘prime’ class to be pre-added. 

Syntax: number.prime? 

Parameter: The function takes the integer which is to be checked for prime or not.

Return Value: The function returns a boolean value which determines if the value is prime or not.

Example 1

Ruby




# Ruby program for prime? function
  
# Pre-defined class
require 'prime'
  
# Initializing the numbers
num1 = 100
num2 = 17
num3 = 90
num4 = 29
     
# Printing if prime or not
puts num1.prime?
puts num2.prime?
puts num3.prime?
puts num4.prime?


Output

false
true
false
true

Example 2

Ruby




# Ruby program for prime? function
  
# Pre-defined class
require 'prime'
  
# Initializing the numbers
num1 = 13
num2 = 19
num3 = 18
num4 = 10
    
# Printing if prime or not
puts num1.prime?
puts num2.prime?
puts num3.prime?
puts num4.prime?


Output

true
true
false
false

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads