src/lcm.js
zzmq.dev@gmail.com/mathkit / src / lcm.js
@ 9271926f
Download
Ladder Summary
- file Implements the `lcm` function which returns the least common multiple of two integer inputs, handling invalid or non‑integer arguments by returning `NaN`, returning `0` if either argument is zero, and calculating the result using the existing `gcd` function while preserving sign correctness.
/**
* Returns the least common multiple (LCM) of two integers.
*
* @param {number} a - First integer.
* @param {number} b - Second integer.
* @returns {number} The LCM, or NaN if either argument is not an integer.
*/
import { gcd } from "./gcd.js";
export function lcm(a, b) {
if (typeof a !== "number" || typeof b !== "number") return NaN;
if (!Number.isInteger(a) || !Number.isInteger(b)) return NaN;
if (a === 0 || b === 0) return 0;
const divisor = gcd(a, b);
return Math.abs((a / divisor) * b);
}