This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
mpz_t _a, _b, _x;
mpz_init_set_str(_a, "123456789", 10);
mpz_init_set_str(_b, "987654321", 10);
mpz_init(_x);
mpz_lcm(_x, _a, _b);
gmp_printf("%Zd\n", _x);
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;
}
gcd(A,B) when A == 0; B == 0 -> 0;
gcd(A,B) when A == B -> A;
gcd(A,B) when A > B -> gcd(A-B, B);
gcd(A,B) -> gcd(A, B-A).
lcm(A,B) -> (A*B) div gcd(A, B).
gcd.GCD(nil, nil, a, b)
x.Div(a, gcd).Mul(x, b)
LCM is not in the standard library, but can be deduced from GCD.
gcd divides a, by definition.
Chaining is permitted and idiomatic.
a, b, gcd, x have pointer type *big.Int.
gcd divides a, by definition.
Chaining is permitted and idiomatic.
a, b, gcd, x have pointer type *big.Int.
sub lcm {
use integer;
my ($x, $y) = @_;
my ($f, $s) = @_;
while ($f != $s) {
($f, $s, $x, $y) = ($s, $f, $y, $x) if $f > $s;
$f = $s / $x * $x;
$f += $x if $f < $s;
}
$f
}
sub gcd {
my ($x, $y) = @_;
while ($x) { ($x, $y) = ($y % $x, $x) }
$y
}
sub lcm {
my ($x, $y) = @_;
($x && $y) and $x / gcd($x, $y) * $y or 0
}