@lucadani7/algonodejs-for-beginners
Version:
Just a simple Node.js package with some basic algorithms perfect for people just starting out. It's got easy-to-understand TypeScript versions of stuff like sorting, searching, math with numbers, and messing with strings.
61 lines (60 loc) • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DivisorsClass = void 0;
class DivisorsClass {
static isDivisibleWithCertainValue(value1, value2) {
return value1 % value2 === 0n;
}
static isEven(value) {
return this.isDivisibleWithCertainValue(value, 2n);
}
static getDivisorsOfCertainValue(value) {
const divisorsArray = [];
for (let i = 1n; i * i <= value; i += 1n) {
if (this.isDivisibleWithCertainValue(value, i)) {
divisorsArray.push(i);
let j = value / i;
if (i !== j) {
divisorsArray.push(j);
}
}
}
return divisorsArray.sort((a, b) => (a < b ? -1 : 1));
}
static countDivisorsOfCertainValue(value) {
return this.getDivisorsOfCertainValue(value).length;
}
static getDivisorsSumOfCertainValue(value) {
let sum = 0n;
for (let divisor of this.getDivisorsOfCertainValue(value)) {
sum += divisor;
}
return sum;
}
static factorizeCertainValue(value) {
const factors = new Map();
let div2 = 0n;
while (this.isEven(value)) {
++div2;
value /= 2n;
}
if (div2 > 0n) {
factors.set(2n, div2);
}
for (let i = 3n; i * i <= value; i += 2n) {
let count = 0n;
while (this.isDivisibleWithCertainValue(value, i)) {
++count;
value /= i;
}
if (count > 0n) {
factors.set(i, count);
}
}
if (value > 1n) {
factors.set(value, 1n);
}
return factors;
}
}
exports.DivisorsClass = DivisorsClass;