1.0.3 • Published 8 months ago

@jlhv/numeric-helper v1.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

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-helper

Usage

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: 36

Functions

Basic Number Operations

    isEven(num: number): boolean

Checks if a number is even.

console.log(isEven(10)); // Output: true
console.log(isEven(7));  // Output: false
isOdd(num: number): boolean
  • Checks if a number is odd.
console.log(isOdd(10)); // Output: false
console.log(isOdd(7));  // Output: true
factorial(num: number): 
  • Returns the factorial of a number.
console.log(factorial(5)); // Output: 120
sum(arr: number[]): number
  • Returns the sum of an array of numbers.
console.log(sum([1, 2, 3, 4, 5])); // Output: 15
average(arr: number[]): number
  • Returns the average of an array of numbers.
console.log(average([2, 4, 6, 8, 10])); // Output: 6

Prime & Divisibility

isPrime(num: number): boolean
  • Checks if a number is prime.
console.log(isPrime(11)); // Output: true
console.log(isPrime(10)); // Output: false
gcd(a: number, b: number): number
  • Finds the greatest common divisor (GCD) of two numbers.
console.log(gcd(28, 35)); // Output: 7
lcm(a: number, b: number): number
  • Finds the least common multiple (LCM) of two numbers.
console.log(lcm(12, 15)); // Output: 60

Conversions

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.14159
radToDeg(radians: number): number
  • Converts radians to degrees.
console.log(radToDeg(3.14159)); // Output: 180

Rounding & 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: 20

Miscellaneous

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 100
power(base: number, exp: number): number
  • Computes the power of a number without using Math.pow().
console.log(power(2, 3)); // Output: 8
sqrt(num: number): number
  • Computes the square root of a number using the Babylonian method.
console.log(sqrt(25)); // Output: 5
nthRoot(num: number, n: number): number
  • Computes the nth root of a number using Newton’s method.
console.log(nthRoot(27, 3)); // Output: 3
isPerfectSquare(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.