@rgsoft/math
Advanced tools
Comparing version 1.0.7 to 1.0.8
@@ -183,3 +183,21 @@ declare class Complex { | ||
declare function lcm(a: number, b: number): number; | ||
/** | ||
* Checks if a number is prime | ||
* @param { number } n | ||
* @returns { boolean } | ||
*/ | ||
declare function prime(n: number): boolean; | ||
/** | ||
* Checks if a number is prime | ||
* @param { number } n | ||
* @returns { number[][] } | ||
*/ | ||
declare function factors(n: number): number[][]; | ||
/** | ||
* Checks if a number is prime | ||
* @param { number } m | ||
* @returns { number } | ||
*/ | ||
declare function totient(m: number): number; | ||
export { Complex, Line, Segment, Vector, gcd, lcm, mod }; | ||
export { Complex, Line, Segment, Vector, factors, gcd, lcm, mod, prime, totient }; |
@@ -27,5 +27,8 @@ "use strict"; | ||
Vector: () => Vector, | ||
factors: () => factors, | ||
gcd: () => gcd, | ||
lcm: () => lcm, | ||
mod: () => mod | ||
mod: () => mod, | ||
prime: () => prime, | ||
totient: () => totient | ||
}); | ||
@@ -442,2 +445,54 @@ module.exports = __toCommonJS(src_exports); | ||
} | ||
function prime(n) { | ||
if (!Number.isInteger(n) || n <= 0) { | ||
throw new Error("The number must be positive integer"); | ||
} | ||
const l = Math.sqrt(n); | ||
for (let i = 2; i <= l; i++) { | ||
if (n % i === 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
function factors(n) { | ||
if (!Number.isInteger(n) || n <= 0) { | ||
throw new Error("The number must be positive integer"); | ||
} | ||
if (n === 1) { | ||
return [[1, 1]]; | ||
} | ||
const factors2 = []; | ||
let i = 2; | ||
let factor = [i, 0]; | ||
while (n > 1) { | ||
if (n % i === 0) { | ||
n /= i; | ||
factor[1]++; | ||
} else { | ||
i++; | ||
if (factor[1] > 0) { | ||
factors2.push(factor); | ||
} | ||
factor = [i, 0]; | ||
} | ||
} | ||
if (factor[1] > 0) { | ||
factors2.push(factor); | ||
} | ||
return factors2; | ||
} | ||
function totient(m) { | ||
if (!Number.isInteger(m) || m <= 0) { | ||
throw new Error("The number must be positive integer"); | ||
} | ||
const factorization = factors(m); | ||
let a = 1; | ||
let b = 1; | ||
factorization.forEach((f) => { | ||
a *= f[0]; | ||
b *= f[0] - 1; | ||
}); | ||
return b * m / a; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -449,6 +504,9 @@ 0 && (module.exports = { | ||
Vector, | ||
factors, | ||
gcd, | ||
lcm, | ||
mod | ||
mod, | ||
prime, | ||
totient | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@rgsoft/math", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Yet another JS math library", | ||
@@ -23,3 +23,7 @@ "main": "./dist/index.js", | ||
"complex-numbers", | ||
"vectors" | ||
"vectors", | ||
"numbers", | ||
"prime-numbers", | ||
"factors", | ||
"totient" | ||
], | ||
@@ -26,0 +30,0 @@ "author": "Ricardo Miranda <rgmiranda@live.com.ar>", |
@@ -270,2 +270,28 @@ # math-js | ||
will ouput `36`. | ||
will ouput `36`. | ||
## Is a Number Prime | ||
The `prime` checks if a positive integer is prime or not | ||
```javascript | ||
console.log(prime(17)); | ||
``` | ||
will ouput `true`. | ||
## Prime Factorization | ||
The `factors` function gets the prime factors with their respective exponents | ||
```javascript | ||
console.log(factors(48)); | ||
``` | ||
will ouput `[ [2, 4], [3, 1] ]`, where each element of the array is another | ||
array with two numbers: | ||
1. The prime factor | ||
2. The exponent of the factor | ||
In the example, this means that `48` is `2^4 + 3^1`. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
81674
1167
296