Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
integer
communitycreator

What is integer.gcdlcm() in Ruby?

okwudili onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 the gcdlcm() method.
  • int2: This is the second variable that has an integer value. It is passed inside the gcdlcm() 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 values
int1 = 20
int2 = 4
int3 = 15
int4 = 5
# get greatest common divisor
# and least common multiple
puts "#{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]
Get the greatest common divisor and the least common multiple of integers

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.

RELATED TAGS

ruby
integer
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring