1.0.3 • Published 8 months ago
@jlhv/numeric-helper v1.0.3
Numeric Helper Library
Overview
The Numeric Helper Library provides a collection of utility functions for numeric operations, including mathematical calculations, validations, and conversions.
Installation
Install the package using:
npm install @jlhv/numeric-helperUsage
Import the required functions from the library:
import { isEven, isOdd, factorial, gcd, lcm } from "@jlhv/numeric-helper";
console.log(isEven(4)); // Output: true
console.log(isOdd(3)); // Output: true
console.log(factorial(5)); // Output: 120
console.log(gcd(12, 18)); // Output: 6
console.log(lcm(12, 18)); // Output: 36Functions
Basic Number Operations
isEven(num: number): booleanChecks if a number is even.
console.log(isEven(10)); // Output: true
console.log(isEven(7)); // Output: falseisOdd(num: number): boolean- Checks if a number is odd.
console.log(isOdd(10)); // Output: false
console.log(isOdd(7)); // Output: truefactorial(num: number): - Returns the factorial of a number.
console.log(factorial(5)); // Output: 120sum(arr: number[]): number- Returns the sum of an array of numbers.
console.log(sum([1, 2, 3, 4, 5])); // Output: 15average(arr: number[]): number- Returns the average of an array of numbers.
console.log(average([2, 4, 6, 8, 10])); // Output: 6Prime & Divisibility
isPrime(num: number): boolean- Checks if a number is prime.
console.log(isPrime(11)); // Output: true
console.log(isPrime(10)); // Output: falsegcd(a: number, b: number): number- Finds the greatest common divisor (GCD) of two numbers.
console.log(gcd(28, 35)); // Output: 7lcm(a: number, b: number): number- Finds the least common multiple (LCM) of two numbers.
console.log(lcm(12, 15)); // Output: 60Conversions
toBinary(num: number): string- Converts a number to binary.
console.log(toBinary(10)); // Output: "1010"toHex(num: number): string- Converts a number to hexadecimal.
console.log(toHex(255)); // Output: "ff"degToRad(degrees: number): number- Converts degrees to radians.
console.log(degToRad(180)); // Output: 3.14159radToDeg(radians: number): number- Converts radians to degrees.
console.log(radToDeg(3.14159)); // Output: 180Rounding & Clamping
clamp(num: number, min: number, max: number): number- Clamps a number within a given range.
console.log(clamp(15, 10, 20)); // Output: 15
console.log(clamp(25, 10, 20)); // Output: 20Miscellaneous
randomInt(min: number, max: number): number- Generates a random integer between min and max.
console.log(randomInt(1, 100)); // Output: Random number between 1 and 100power(base: number, exp: number): number- Computes the power of a number without using Math.pow().
console.log(power(2, 3)); // Output: 8sqrt(num: number): number- Computes the square root of a number using the Babylonian method.
console.log(sqrt(25)); // Output: 5nthRoot(num: number, n: number): number- Computes the nth root of a number using Newton’s method.
console.log(nthRoot(27, 3)); // Output: 3isPerfectSquare(num: number): boolean- Checks if a number is a perfect square.
console.log(isPerfectSquare(36)); // Output: true
console.log(isPerfectSquare(37)); // Output: false License
ISC License.
Author
Vijayavel R.