programming-examples/js/Math/Least common multiple (LCM) of two numbers.js

18 lines
450 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
function lcm_two_numbers(x, y) {
if ((typeof x !== 'number') || (typeof y !== 'number'))
return false;
return (!x || !y) ? 0 : Math.abs((x * y) / gcd_two_numbers(x, y));
}
function gcd_two_numbers(x, y) {
x = Math.abs(x);
y = Math.abs(y);
while(y) {
var t = y;
y = x % y;
x = t;
}
return x;
}
console.log(lcm_two_numbers(3,15));
console.log(lcm_two_numbers(10,15));