divisor
Version:
Calculates divisors/factors of natural numbers and gcd/lcm using euclid's algorithm.
189 lines (183 loc) • 6.39 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
countCommonDivisors: () => countCommonDivisors,
countDivisors: () => countDivisors,
getCommonDivisors: () => getCommonDivisors,
getDivisors: () => getDivisors,
greatestCommonDivisor: () => greatestCommonDivisor,
greatestProperDivisor: () => greatestProperDivisor,
leastCommonMultiple: () => leastCommonMultiple,
multiplyCommonDivisors: () => multiplyCommonDivisors,
multiplyDivisors: () => multiplyDivisors,
smallestProperDivisor: () => smallestProperDivisor,
sumCommonDivisors: () => sumCommonDivisors,
sumDivisors: () => sumDivisors
});
module.exports = __toCommonJS(src_exports);
// 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;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
countCommonDivisors,
countDivisors,
getCommonDivisors,
getDivisors,
greatestCommonDivisor,
greatestProperDivisor,
leastCommonMultiple,
multiplyCommonDivisors,
multiplyDivisors,
smallestProperDivisor,
sumCommonDivisors,
sumDivisors
});
;