es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
17 lines (16 loc) • 418 B
JavaScript
/**
* Calculates the greatest common divisor (GCD) of two numbers using the Euclidean algorithm.
* @param {number} a - The first number.
* @param {number} b - The second number.
* @returns The greatest common divisor of a and b.
* @example
* const result = gcd(48, 18); // 6
*/
export function gcd(a, b) {
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}