Skip to content

Instantly share code, notes, and snippets.

@gkucmierz
Created September 12, 2019 14:06
Show Gist options
  • Select an option

  • Save gkucmierz/d90ef1d39aa2393d6bc1c31e9b8f683f to your computer and use it in GitHub Desktop.

Select an option

Save gkucmierz/d90ef1d39aa2393d6bc1c31e9b8f683f to your computer and use it in GitHub Desktop.
lcm, gcd
// Smallest Common Multiple
// Least common multiple
// Greatest common divisor
function gcd(a, b) {
if (a < 0) a = -a;
if (b < 0) b = -b;
if (b > a) {
[a, b] = [b, a];
}
while (1) {
if (b == 0) return a;
a %= b;
if (a == 0) return b;
b %= a;
}
}
function lcm(a, b) {
return a * b / gcd(a, b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment