Ruby 整数 gcd()函数与示例
Ruby 中的gcd()函数返回两个数字的 gcd。 GCD 表示将两个数字相除的最大公约数。
Syntax: number1.gcd(number2)
Parameter: The function requires two numbers whose gcd is to be returned.
Return Value: The function returns the gcd of two numbers
示例 #1:
# Ruby program of Integer gcd() function
# Initializing the numbers
num1 = 10
num2 = 15
num3 = 21
num4 = 14
# Prints the gcd value
puts num1.gcd(num2)
puts num3.gcd(num4)
输出:
5
7
示例 #2:
# Ruby program of Integer gcd() function
# Initializing the numbers
num1 = 22
num2 = 18
num3 = 30
num4 = 25
# Prints the gcd value
puts num1.gcd(num2)
puts num3.gcd(num4)
输出:
2
5