QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Least Common Multiple

Calculates the least common multiple (lcm) of two numbers

  • math
  • number
  • least-common-multiple
  • lcm
  • euclidean-algorithm
  • number-theory
  • algebra

Install

npx shadcn@latest add https://quicksnip.dev/r/least-common-multiple.json

This snippet is not verified yet. Review it before use, or help verify it.

Code

function lcm(a, b) {
    function gcd(x, y) {
        while (y !== 0) {
            const temp = y;
            y = x % y;
            x = temp;
        }
        return Math.abs(x);
    }
    return Math.abs(a * b) / gcd(a, b);
}

Usage

int a = 16;
int b = 12;
System.out.println(lcm(a, b)); // 48