This language bar is your friend. Select your favorite languages!

Idiom #75 Compute LCM

Compute the least common multiple x of big integers a and b. Use an integer type able to handle huge numbers.

x = lcm(a, b);

int lcm(int a, int b) => (a * b) ~/ gcd(a, b);

int gcd(int a, int b) {
  while (b != 0) {
    var t = b;
    b = a % t;
    a = t;
  }
  return a;
}
defmodule BasicMath do
	def gcd(a, 0), do: a
	def gcd(0, b), do: b
	def gcd(a, b), do: gcd(b, rem(a,b))
	
	def lcm(0, 0), do: 0
	def lcm(a, b), do: (a*b)/gcd(a,b)
end
import "math/big"
gcd.GCD(nil, nil, a, b)
x.Div(a, gcd).Mul(x, b)
x = lcm a b
from fractions import gcd
x = (a*b)//gcd(a, b)
x = a.lcm(b)
extern crate num;

use num::Integer;
use num::bigint::BigInt;
let x = a.lcm(&b);

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

deleplace

Related idioms