红宝石整数素数?示例函数
总理? Ruby 中的函数返回一个布尔值。如果数字是素数,则返回 true,否则返回 false。它需要预先添加“prime”类。
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.
示例 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?
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?
输出:
false
true
false
true
示例 2 :
红宝石
# 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?
输出:
true
true
false
false