Ruby gcdlcm() Method

Ruby gcdlcm() Function with Examples

Ruby gcdlcm() method takes two integer numbers (one as an object and another as a parameter) and returns an array of the GCD and LCM of that two numbers gradually. Where mathematically GCD consists of the greatest common divisor and LCM consists of the least common multiple.

The both values are returned as an array. Applying other data types with this method including double will raise an error.

Ruby integer? Method

Syntaxnum1.gcdlcm(num2) or num2.gcdlcm(num1)
Where num1 and num2 the values for calculating the GCD and LCM values.
ParametersThis method takes an integer value as a parameter and another number is taken as an object.
Return valueReturns an array of GCD and LCM value for the specified values.

Example Program-1:

num1 = 15
num2 = 10
num3 = 28
num4 = 7

x = num1.gcdlcm(num2)
y = num4.gcdlcm(num3)
z = 52.gcdlcm(13)

puts "#{x}"
puts "#{y}"
puts "#{z}"

Output

[5, 30]
[7, 28]
[13, 52]

Example Program-2:

num1 = 7
num2 = 12
num3 = 15
num4 = 16

puts "#{num1.gcdlcm(num2)}"
puts "#{num3.gcdlcm(num4)}"
puts "#{5.gcdlcm(7)}"

Output

[1, 84]
[1, 240]
[1, 35
Posted in