UNPKG

divisor

Version:

Calculates divisors/factors of natural numbers and gcd/lcm using euclid's algorithm.

151 lines (147 loc) 4.84 kB
// src/helpers/divisor.helpers.ts var getAllDivisors = (n) => { const squareRoot = Math.sqrt(n); const divisors = []; for (let i = 0; i <= squareRoot; i += 1) { if (n % i === 0) { const divisor1 = i; const divisor2 = n / i; if (divisor1 === divisor2) divisors.push(divisor1); else divisors.push(divisor1, divisor2); } } return divisors; }; // src/helpers/parameter-integration.helpers.ts var isPositive = (n) => n > 0; var isWholeNumber = (n) => Number.isInteger(n); var isSortExpressionTrue = (sort) => { if (!sort) return true; if (sort === "asc" || sort === "desc") return true; return false; }; var checkDivisorsParameterValidity = (n, options) => { const errors = []; if (isPositive(n) === false) errors.push("Provided number must be a positive number."); if (isWholeNumber(n) === false) errors.push("Provided number must be a whole number."); if (options !== void 0 && isSortExpressionTrue(options.sort) === false) errors.push( "sort expression can be defined as 'asc', 'desc' or left 'undefined" ); if (errors.length > 0) throw errors; }; var checkCommonDivisorsParameterValidity = (n1, n2, sort) => { const errors = []; if (isPositive(n1) === false || isPositive(n2) === false) errors.push("Provided numbers must be positive numbers."); if (isWholeNumber(n1) === false || isWholeNumber(n2) === false) errors.push("Provided numbers must be whole numbers."); if (sort !== void 0 && isSortExpressionTrue(sort) === false) errors.push( "sort expression can be defined as 'asc', 'desc' or left 'undefined" ); if (errors.length > 0) throw errors; }; // src/common-divisors.ts var gcd = (n1, n2) => { if (n1 === 0) return n2; return gcd(n2 % n1, n1); }; function getCommonDivisors(n1, n2, sort) { checkCommonDivisorsParameterValidity(n1, n2); const _gcd = gcd(n1, n2); let divisors = getAllDivisors(_gcd); if (sort) { if (sort === "asc") divisors = divisors.sort((a, b) => a > b ? 1 : -1); else divisors = divisors.sort((a, b) => a > b ? -1 : 1); } return divisors; } function greatestCommonDivisor(n1, n2) { checkCommonDivisorsParameterValidity(n1, n2); return gcd(n1, n2); } function leastCommonMultiple(n1, n2) { checkCommonDivisorsParameterValidity(n1, n2); return n1 / gcd(n1, n2) * n2; } function countCommonDivisors(n1, n2) { checkCommonDivisorsParameterValidity(n1, n2); const _gcd = gcd(n1, n2); const divisors = getAllDivisors(_gcd); return divisors.length; } function multiplyCommonDivisors(n1, n2) { checkCommonDivisorsParameterValidity(n1, n2); const _gcd = gcd(n1, n2); const divisors = getAllDivisors(_gcd); return divisors.reduce((a, b) => a * b, 1); } function sumCommonDivisors(n1, n2) { checkCommonDivisorsParameterValidity(n1, n2); const _gcd = gcd(n1, n2); const divisors = getAllDivisors(_gcd); return divisors.reduce((a, b) => a + b, 0); } // src/divisors.ts function getDivisors(n, options) { checkDivisorsParameterValidity(n, options); let sort; let onlyProperDivisors; if (options) ({ sort, onlyProperDivisors } = options); else onlyProperDivisors = false; let divisors = getAllDivisors(n); if (onlyProperDivisors) divisors = divisors.filter((divisor) => divisor !== n); if (sort) { if (sort === "asc") divisors = divisors.sort((a, b) => a > b ? 1 : -1); else divisors = divisors.sort((a, b) => a > b ? -1 : 1); } return divisors; } function countDivisors(n, onlyProperDivisors = false) { checkDivisorsParameterValidity(n); const divisors = getAllDivisors(n); const divisorsLength = divisors.length; return onlyProperDivisors ? divisorsLength - 1 : divisorsLength; } function multiplyDivisors(n, onlyProperDivisors = false) { checkDivisorsParameterValidity(n); let divisors = getAllDivisors(n); if (onlyProperDivisors) divisors = divisors.filter((divisor) => divisor !== n); return divisors.reduce((a, b) => a * b, 1); } function sumDivisors(n, onlyProperDivisors = false) { checkDivisorsParameterValidity(n); let divisors = getAllDivisors(n); if (onlyProperDivisors) divisors = divisors.filter((divisor) => divisor !== n); return divisors.reduce((a, b) => a + b, 0); } function greatestProperDivisor(n) { checkDivisorsParameterValidity(n); const properDivisors = getAllDivisors(n).filter((divisor) => divisor !== n); if (properDivisors.length === 0) return null; return Math.max(...properDivisors); } function smallestProperDivisor(n) { checkDivisorsParameterValidity(n); return n === 1 ? null : 1; } export { countCommonDivisors, countDivisors, getCommonDivisors, getDivisors, greatestCommonDivisor, greatestProperDivisor, leastCommonMultiple, multiplyCommonDivisors, multiplyDivisors, smallestProperDivisor, sumCommonDivisors, sumDivisors };