Here's a README file for your math-easy
npm module:
math-easy
is a lightweight npm module that provides a collection of simple and useful mathematical functions. From factorials and prime number checking to geometric and harmonic means, math-easy
covers a wide range of mathematical needs, with built-in error handling and type checking.
To install the module, use npm:
npm install @mjmaurya/math-easy
After installation, you can import and use any of the available functions in your project.
const math = require('@mjmaurya/math-easy');
// Factorial of 5
console.log(math.factorial(5)); // Output: 120
// Check if a number is prime
console.log(math.isPrime(7)); // Output: true
// Calculate the GCD of two numbers
console.log(math.gcd(24, 36)); // Output: 12
// Find the LCM of two numbers
console.log(math.lcm(12, 15)); // Output: 60
// Generate Fibonacci sequence up to nth position
console.log(math.fibonacci(7)); // Output: [0, 1, 1, 2, 3, 5, 8, 13]
// Calculate the average of an array of numbers
console.log(math.average([1, 2, 3, 4, 5])); // Output: 3
Calculates the factorial of a non-negative integer.
math.factorial(n);
- n: Non-negative integer
-
Returns: Factorial of
n
Checks if a number is a prime number.
math.isPrime(num);
- num: Integer greater than or equal to 2
-
Returns:
true
ifnum
is prime,false
otherwise
Calculates the greatest common divisor (GCD) of two integers.
math.gcd(a, b);
- a, b: Integers
-
Returns: GCD of
a
andb
Calculates the least common multiple (LCM) of two integers.
math.lcm(a, b);
- a, b: Integers
-
Returns: LCM of
a
andb
Generates the Fibonacci sequence up to the nth number.
math.fibonacci(n);
- n: Non-negative integer
-
Returns: Array of Fibonacci numbers up to
n
Generates a random integer between min
and max
.
math.randomInt(min, max);
- min, max: Integers
-
Returns: Random integer between
min
andmax
Calculates the sum of all elements in an array of numbers.
math.sumArray(arr);
- arr: Array of numbers
-
Returns: Sum of elements in
arr
Calculates the average of an array of numbers.
math.average(arr);
- arr: Array of numbers
-
Returns: Average of elements in
arr
Calculates the standard deviation of an array of numbers.
math.standardDeviation(arr);
- arr: Array of numbers
-
Returns: Standard deviation of elements in
arr
Calculates the median of an array of numbers.
math.median(arr);
- arr: Array of numbers
-
Returns: Median of elements in
arr
Finds the mode(s) in an array of numbers.
math.mode(arr);
- arr: Array of numbers
-
Returns: Array of mode(s) from
arr
Calculates the result of raising a base to an exponent.
math.power(base, exponent);
- base, exponent: Numbers
-
Returns:
base
raised to the power ofexponent
Calculates the nth root of a number.
math.nthRoot(num, n);
- num: Number
- n: The root
-
Returns: The nth root of
num
Calculates the logarithm of a number with a specified base.
math.logBase(num, base);
- num, base: Numbers
-
Returns: Logarithm of
num
to the basebase
Clamps a number between two bounds.
math.clamp(num, min, max);
- num, min, max: Numbers
-
Returns:
num
clamped betweenmin
andmax
Calculates the hypotenuse of a right triangle given the lengths of the two legs.
math.pythagorean(a, b);
- a, b: Lengths of the two legs
- Returns: Hypotenuse of the triangle
Calculates the Euclidean distance between two points in n-dimensional space.
math.euclideanDistance(pointA, pointB);
- pointA, pointB: Arrays of coordinates
-
Returns: Euclidean distance between
pointA
andpointB
Converts degrees to radians.
math.degreesToRadians(degrees);
- degrees: Angle in degrees
- Returns: Angle in radians
Converts radians to degrees.
math.radiansToDegrees(radians);
- radians: Angle in radians
- Returns: Angle in degrees
Solves the quadratic equation ax² + bx + c = 0 and returns the roots.
math.quadraticRoots(a, b, c);
- a, b, c: Coefficients of the quadratic equation
- Returns: Array of two roots
Calculates the number of permutations of n
items taken r
at a time.
math.permutation(n, r);
- n, r: Non-negative integers
-
Returns: Number of permutations of
n
takenr
at a time
Calculates the number of combinations of n
items taken r
at a time.
math.combination(n, r);
- n, r: Non-negative integers
-
Returns: Number of combinations of
n
takenr
at a time
Calculates the geometric mean of an array of positive numbers.
math.geometricMean(arr);
- arr: Array of positive numbers
-
Returns: Geometric mean of elements in
arr
Calculates the harmonic mean of an array of positive numbers.
math.harmonicMean(arr);
- arr: Array of positive numbers
-
Returns: Harmonic mean of elements in
arr
Each function has built-in error handling to ensure correct usage. For instance, if a function expects an array of numbers, it will throw an error if passed an invalid argument.
MIT License
This provides a comprehensive README that explains how to use your npm module math-easy
. You can expand or customize this as needed.