Files
30-seconds-of-code/snippets/least-common-multiple-(LCM).md
2017-12-16 20:55:39 +01:00

392 B

Least common multiple (LCM)

Use this lcm formula lcm(a,b)=|a*b|/gcd(a,b) for calculating the least common multiple of two numbers. Makes use of the GCD snippet.

const lcm = (x,y) => Math.abs(x*y)/(gcd(x,y));
const gcd = (x, y) => !y ? x : gcd(y, x % y);
// lcm(10,5) -> 10
// lcm(12,7) -> 84