📜  红宝石整数素数?示例函数

📅  最后修改于: 2022-05-13 01:54:27.278000             🧑  作者: Mango

红宝石整数素数?示例函数

总理? Ruby 中的函数返回一个布尔值。如果数字是素数,则返回 true,否则返回 false。它需要预先添加“prime”类。

示例 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