InferenceAI

Enter at least two characters to search.

Start free

GCD and LCM

easyMath15 min10 XP

Given two positive integers a and b, return [g, l] where g=gcd⁡(a,b)g = \gcd(a, b) and l=lcm⁡(a,b)l = \operatorname{lcm}(a, b).

Use the identity gcd⁡(a,b)⋅lcm⁡(a,b)=a⋅b\gcd(a, b) \cdot \operatorname{lcm}(a, b) = a \cdot b.

Requirements

  • Implement Euclid's algorithm yourself: gcd⁡(a,b)=gcd⁡(b,a mod b)\gcd(a, b) = \gcd(b, a \bmod b).

Constraints

  • 1≤a,b≤1091 \le a, b \le 10^9

Examples

EXAMPLE WALKTHROUGHcommon factors

A guided view of the published example, not a trace of your code.

INPUT[12,18]
TASKgcd_and_lcm()
EXPECTED OUTPUT[6,36]

Start with the input values shown in this example.

Example 1 of 3

Input: a = 12, b = 18

Output: [6,36]

Explanation: common factors

Input: a = 7, b = 13

Output: [1,91]

Explanation: coprime

Input: a = 5, b = 5

Output: [5,5]

Explanation: equal

Hint 1

Repeatedly replace (a, b) with (b, a % b) until b is 0.

Hint 2

Divide before multiplying to keep the LCM computation small: a // g * b.

Topics

DSAMathPython