Skip to content

Commit

Permalink
Smath v1.6.1 (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicfv authored Mar 27, 2024
1 parent f920526 commit 4c5c7c8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
4 changes: 4 additions & 0 deletions smath/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.6.1

- Add `factors()` function to compute a list of prime factors of any number

## 1.6.0

- Add `lim()`, `differentiate()`, and `integrate()`
Expand Down
2 changes: 1 addition & 1 deletion smath/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"clean": "rm -rf node_modules package-lock.json"
},
"dependencies": {
"smath": "file:smath-1.6.0.tgz"
"smath": "file:smath-1.6.1.tgz"
}
}
2 changes: 1 addition & 1 deletion smath/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "smath",
"version": "1.6.0",
"version": "1.6.1",
"description": "Small math function library",
"homepage": "https://npm.nicfv.com/smath",
"bin": "dist/bin.js",
Expand Down
5 changes: 5 additions & 0 deletions smath/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ if (func.includes('help')) {
console.log(' translate <n> <min1> <max1> <min2> <max2>');
console.log(' : Linearly interpolate `n` from `min1`, `max1` to `min2`, `max2`');
console.log(' factorial <n> : Compute `n!` (factorial)');
console.log(' factors <n> : List the prime factors of `n`');
console.log(' error <exp> <act> : Calculate the normaized percent error between `exp` and `act`');
console.log(' sum <c0> [c1] ... [cn] : Compute a total of `n` numbers');
console.log(' prod <c0> [c1] ... [cn] : Compute a product of `n` numbers');
Expand Down Expand Up @@ -71,6 +72,10 @@ switch (func) {
console.log(SMath.factorial(nums[0]));
break;
}
case ('factors'): {
console.log(SMath.factors(nums[0]));
break;
}
case ('error'): {
console.log(SMath.error(nums[0], nums[1]));
break;
Expand Down
28 changes: 28 additions & 0 deletions smath/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,34 @@ export abstract class SMath {
return n * this.factorial(n - 1);
}
}
/**
* Factorize `n` into its prime factors.
* @param n Any positive integer
* @returns The array of prime factors
* @example
* ```js
* const y = SMath.factors(12); // [ 2, 2, 3 ]
* ```
*/
public static factors(n: number): Array<number> {
if (n < 0 || (n | 0) !== n) {
throw new Error('Input must be a positive integer!');
}
if (n <= 3) {
return [n];
}
const f: Array<number> = [];
let i: number = 2;
while (n > 1 && i <= n) {
if ((n / i) === ((n / i) | 0)) {
n /= i;
f.push(i);
} else {
i++;
}
}
return f;
}
/**
* Calculate the relative normalized error or deviation from any
* value to an accepted value. An error of 0 indicates that the
Expand Down
13 changes: 13 additions & 0 deletions smath/src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ X.eq(SMath.factorial(3), 6);
X.eq(SMath.factorial(4), 24);
X.eq(SMath.factorial(5), 120);

X.is(SMath.factors(1).join(), '1');
X.is(SMath.factors(2).join(), '2');
X.is(SMath.factors(3).join(), '3');
X.is(SMath.factors(4).join(), '2,2');
X.is(SMath.factors(5).join(), '5');
X.is(SMath.factors(6).join(), '2,3');
X.is(SMath.factors(7).join(), '7');
X.is(SMath.factors(8).join(), '2,2,2');
X.is(SMath.factors(24).join(), '2,2,2,3');
for (let i = 1; i <= 100; i++) {
X.eq(SMath.prod(SMath.factors(i)), i);
}

X.eq(SMath.error(9, 10), -0.1);
X.eq(SMath.error(11, 10), 0.1);
X.eq(SMath.error(-1, 2), -1.5);
Expand Down

0 comments on commit 4c5c7c8

Please sign in to comment.