GCD and LCM

Easy~10 min

Given two positive integers a and b, return their greatest common divisor (GCD) and least common multiple (LCM) as a two-element array [gcd, lcm].

The GCD is the largest positive integer that divides both a and b. The LCM is the smallest positive integer that is divisible by both a and b.

For example, gcdLcm(12, 8) returns [4, 24] because the GCD of 12 and 8 is 4, and their LCM is 24.

Examples

Example 1
Input: a = 12, b = 8
Output: [4, 24]
Explanation: GCD(12, 8) = 4. LCM = 12 × 8 / 4 = 24.
Example 2
Input: a = 7, b = 3
Output: [1, 21]
Explanation: 7 and 3 are coprime (GCD = 1), so LCM = 7 × 3 = 21.
Example 3
Input: a = 6, b = 6
Output: [6, 6]
Explanation: When both numbers are equal, GCD = LCM = that number.

Constraints

  • 1 <= a, b <= 10^9
  • Both a and b are positive integers.
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run