What is integer.gcdlcm() in Ruby?
Overview
The gcdlcm() integer method returns an array that has the greatest common divisor and the least common multiple of two integers, that is, [gcd, lcm].
Syntax
int1.gcdlcm(int2)
Parameters
int1: This is the variable that has an integer value that invokes thegcdlcm()method.int2: This is the second variable that has an integer value. It is passed inside thegcdlcm()method.
Return value
The value returned is an array that contains the greatest common divisor and the least common multiple of int1 and int2, that is, [gcd, lcm].
Code example
# create integer valuesint1 = 20int2 = 4int3 = 15int4 = 5# get greatest common divisor# and least common multipleputs "#{int1.gcdlcm(4)}" # 20 and 4 = [4,20]puts "#{int2.gcdlcm(2)}" # 4 and 2 = [2,4]puts "#{int3.gcdlcm(3)}" # 15 and 3 = [3,15]puts "#{int4.gcdlcm(100)}" # 5 and 100 = [5,100]
Code explanation
- Lines 2–5: We create some integer values.
- Lines 9–12: We use the
gcdlcm()method to get the greatest common divisor and the least common multiple. Then, we print the results to the console.