@jlhv/numeric-helper
Version:
A simple utility library for numeric manipulation.
186 lines (185 loc) • 4.87 kB
JavaScript
;
/**
* Numeric Helper Library
* Provides utility functions for numeric manipulation.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.isEven = isEven;
exports.isOdd = isOdd;
exports.isPrime = isPrime;
exports.factorial = factorial;
exports.fibonacci = fibonacci;
exports.sum = sum;
exports.average = average;
exports.gcd = gcd;
exports.lcm = lcm;
exports.clamp = clamp;
exports.toBinary = toBinary;
exports.toHex = toHex;
exports.randomInt = randomInt;
exports.isNumeric = isNumeric;
exports.degToRad = degToRad;
exports.radToDeg = radToDeg;
exports.power = power;
exports.sqrt = sqrt;
exports.nthRoot = nthRoot;
exports.isPerfectSquare = isPerfectSquare;
/** Checks if a number is even. */
function isEven(num) {
return num % 2 === 0;
}
/** Checks if a number is odd. */
function isOdd(num) {
return num % 2 !== 0;
}
/** Checks if a number is prime (without using Math.sqrt). */
function isPrime(num) {
if (num <= 1)
return false;
for (let i = 2; i * i <= num; i++) {
if (num % i === 0)
return false;
}
return true;
}
/** Calculates the factorial of a number (non-recursive). */
function factorial(num) {
if (num < 0)
return 0;
let result = 1;
for (let i = 2; i <= num; i++) {
result *= i;
}
return result;
}
/** Returns the nth Fibonacci number (without recursion). */
function fibonacci(n) {
if (n <= 0)
return 0;
if (n === 1)
return 1;
let a = 0, b = 1, temp;
for (let i = 2; i <= n; i++) {
temp = a + b;
a = b;
b = temp;
}
return b;
}
/** Returns the sum of an array of numbers. */
function sum(arr) {
let total = 0;
for (const num of arr) {
total += num;
}
return total;
}
/** Returns the average of an array of numbers. */
function average(arr) {
return arr.length === 0 ? 0 : sum(arr) / arr.length;
}
/** Finds the greatest common divisor (GCD) without using modulo. */
function gcd(a, b) {
while (b !== 0) {
let temp = b;
b = a - Math.floor(a / b) * b;
a = temp;
}
return a;
}
/** Finds the least common multiple (LCM) using GCD. */
function lcm(a, b) {
return (a * b) / gcd(a, b);
}
/** Restricts a number between a min and max value. */
function clamp(num, min, max) {
return num < min ? min : num > max ? max : num;
}
/** Converts a number to binary (without built-in functions). */
function toBinary(num) {
if (num === 0)
return "0";
let binary = "";
let n = num;
while (n > 0) {
binary = (n % 2) + binary;
n = Math.floor(n / 2);
}
return binary;
}
/** Converts a number to hexadecimal (without built-in functions). */
function toHex(num) {
const hexChars = "0123456789abcdef";
if (num === 0)
return "0";
let hex = "";
let n = num;
while (n > 0) {
hex = hexChars[n % 16] + hex;
n = Math.floor(n / 16);
}
return hex;
}
/** Generates a random integer between min and max (without Math.random). */
function randomInt(min, max) {
let seed = Date.now() % 10000;
seed = (seed * 9301 + 49297) % 233280;
let rnd = seed / 233280;
return Math.floor(rnd * (max - min + 1)) + min;
}
/** Checks if a value is numeric. */
function isNumeric(value) {
return typeof value === "number" && isFinite(value);
}
/** Converts degrees to radians (without using Math.PI). */
function degToRad(degrees) {
const piApprox = 355 / 113; // Approximation of π
return (degrees * piApprox) / 180;
}
/** Converts radians to degrees (without using Math.PI). */
function radToDeg(radians) {
const piApprox = 355 / 113;
return (radians * 180) / piApprox;
}
/** Computes the power of a number (base^exp) without using Math.pow. */
function power(base, exp) {
let result = 1;
for (let i = 0; i < exp; i++) {
result *= base;
}
return result;
}
/** Computes the square root of a number using the Babylonian method. */
function sqrt(num) {
if (num < 0)
return NaN;
let guess = num / 2;
let prevGuess;
do {
prevGuess = guess;
guess = (guess + num / guess) / 2;
} while (Math.abs(guess - prevGuess) > 0.00001);
return guess;
}
/** Computes the nth root of a number using Newton's method. */
function nthRoot(num, n) {
if (num < 0 && n % 2 === 0)
return NaN;
let guess = num / n;
let prevGuess;
do {
prevGuess = guess;
guess = ((n - 1) * guess + num / power(guess, n - 1)) / n;
} while (Math.abs(guess - prevGuess) > 0.00001);
return guess;
}
/** Checks if a number is a perfect square (without Math.sqrt). */
function isPerfectSquare(num) {
let x = 1;
while (x * x <= num) {
if (x * x === num)
return true;
x++;
}
return false;
}