Comparing version 1.6.0 to 1.6.1
@@ -29,2 +29,3 @@ #!/usr/bin/env node | ||
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`'); | ||
@@ -74,2 +75,6 @@ console.log(' sum <c0> [c1] ... [cn] : Compute a total of `n` numbers'); | ||
} | ||
case ('factors'): { | ||
console.log(_1.SMath.factors(nums[0])); | ||
break; | ||
} | ||
case ('error'): { | ||
@@ -76,0 +81,0 @@ console.log(_1.SMath.error(nums[0], nums[1])); |
@@ -160,2 +160,31 @@ "use strict"; | ||
/** | ||
* 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 ] | ||
* ``` | ||
*/ | ||
SMath.factors = function (n) { | ||
if (n < 0 || (n | 0) !== n) { | ||
throw new Error('Input must be a positive integer!'); | ||
} | ||
if (n <= 3) { | ||
return [n]; | ||
} | ||
var f = []; | ||
var i = 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 | ||
@@ -162,0 +191,0 @@ * value to an accepted value. An error of 0 indicates that the |
{ | ||
"name": "smath", | ||
"version": "1.6.0", | ||
"version": "1.6.1", | ||
"description": "Small math function library", | ||
@@ -5,0 +5,0 @@ "homepage": "https://npm.nicfv.com/smath", |
@@ -5,3 +5,3 @@ [Home](https://npm.nicfv.com/) | [Docs](https://npm.nicfv.com/smath/) | [GitHub](https://github.com/nicfv/npm/tree/main/smath/) | [npm](https://www.npmjs.com/package/smath) | [Changelog](https://github.com/nicfv/npm/blob/main/smath//CHANGELOG.md) | [YouTube](https://www.youtube.com/@nciv) | Small math function library | ||
![NPM Version](https://img.shields.io/npm/v/smath) | ||
![Relative date](https://img.shields.io/date/1711573124) | ||
![Relative date](https://img.shields.io/date/1711574450) | ||
![GitHub watchers](https://img.shields.io/github/watchers/nicfv/npm) | ||
@@ -16,3 +16,3 @@ ![GitHub forks](https://img.shields.io/github/forks/nicfv/npm) | ||
```shell | ||
npm i smath@1.6.0 | ||
npm i smath@1.6.1 | ||
``` | ||
@@ -19,0 +19,0 @@ |
@@ -114,2 +114,12 @@ /** | ||
/** | ||
* 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 ] | ||
* ``` | ||
*/ | ||
static factors(n: number): Array<number>; | ||
/** | ||
* Calculate the relative normalized error or deviation from any | ||
@@ -116,0 +126,0 @@ * value to an accepted value. An error of 0 indicates that the |
32741
802