@rgsoft/math
Advanced tools
Comparing version 1.0.8 to 1.1.0
@@ -170,2 +170,17 @@ declare class Complex { | ||
/** | ||
* Converts an integer to its string representation on a given base | ||
* @param { number } n Number | ||
* @param { number } b Base | ||
* @returns { string } | ||
*/ | ||
declare function toBase(n: number, b: number): string; | ||
/** | ||
* Converts a string representing a number in a certain base to an integer | ||
* @param { string } d Digits | ||
* @param { number } b Base | ||
* @returns { string } | ||
*/ | ||
declare function fromBase(d: string, b: number): number; | ||
declare function mod(n: number, m: number): number; | ||
@@ -191,3 +206,3 @@ /** | ||
/** | ||
* Checks if a number is prime | ||
* Get the prime factorization of a positive integer | ||
* @param { number } n | ||
@@ -198,3 +213,3 @@ * @returns { number[][] } | ||
/** | ||
* Checks if a number is prime | ||
* Gets the totient of a posite integer | ||
* @param { number } m | ||
@@ -204,3 +219,16 @@ * @returns { number } | ||
declare function totient(m: number): number; | ||
/** | ||
* Generates de Collatz sequence | ||
* @param { number } n | ||
* @param { number } limit | ||
* @returns { number } | ||
*/ | ||
declare function collatz(n: number, limit?: number): number[]; | ||
/** | ||
* Gets the digital roots of a positive integer | ||
* @param { number } n | ||
* @returns { number } | ||
*/ | ||
declare function digitalRoots(n: number): number; | ||
export { Complex, Line, Segment, Vector, factors, gcd, lcm, mod, prime, totient }; | ||
export { Complex, Line, Segment, Vector, collatz, digitalRoots, factors, fromBase, gcd, lcm, mod, prime, toBase, totient }; |
@@ -27,3 +27,6 @@ "use strict"; | ||
Vector: () => Vector, | ||
collatz: () => collatz, | ||
digitalRoots: () => digitalRoots, | ||
factors: () => factors, | ||
fromBase: () => fromBase, | ||
gcd: () => gcd, | ||
@@ -33,2 +36,3 @@ lcm: () => lcm, | ||
prime: () => prime, | ||
toBase: () => toBase, | ||
totient: () => totient | ||
@@ -411,2 +415,52 @@ }); | ||
// src/bases.ts | ||
var alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""); | ||
function toBase(n, b) { | ||
if (!Number.isInteger(n) || n < 0) { | ||
throw new Error("The number must be positive integer"); | ||
} | ||
if (!Number.isInteger(b) || b <= 0) { | ||
throw new Error("The base must be a positive integer"); | ||
} | ||
if (b < 2) { | ||
throw new Error("The min base is 2"); | ||
} | ||
if (b > alphabet.length) { | ||
throw new Error(`The max base allowed is ${alphabet.length}`); | ||
} | ||
let e = 1; | ||
const digits = []; | ||
while (n / (e * b) >= 1) { | ||
e *= b; | ||
} | ||
do { | ||
digits.push(alphabet[Math.floor(n / e)]); | ||
n = n % e; | ||
e /= b; | ||
} while (e >= 1); | ||
return digits.join(""); | ||
} | ||
function fromBase(d, b) { | ||
if (!Number.isInteger(b) || b <= 0) { | ||
throw new Error("The base must be a positive integer"); | ||
} | ||
if (b < 2) { | ||
throw new Error("The min base is 2"); | ||
} | ||
if (b > alphabet.length) { | ||
throw new Error(`The max base allowed is ${alphabet.length}`); | ||
} | ||
const digits = d.split("").reverse(); | ||
return digits.reduce((prev, curr, e) => { | ||
const n = alphabet.indexOf(curr); | ||
if (n < 0) { | ||
throw new Error(`Digit ${curr} not found`); | ||
} | ||
if (n >= b) { | ||
throw new Error(`Invalid digit ${curr} for base ${b}`); | ||
} | ||
return prev + n * b ** e; | ||
}, 0); | ||
} | ||
// src/number.ts | ||
@@ -429,2 +483,5 @@ function mod(n, m) { | ||
} | ||
if (a === b) { | ||
return a; | ||
} | ||
let d; | ||
@@ -500,2 +557,33 @@ do { | ||
} | ||
function collatz(n, limit = 1e4) { | ||
if (!Number.isInteger(n) || n <= 0) { | ||
throw new Error("The number must be positive integer"); | ||
} | ||
const seq = [n]; | ||
for (let i = 1; i < limit; i++) { | ||
if (n === 1) { | ||
break; | ||
} | ||
if (n % 2 === 0) { | ||
n = n * 0.5; | ||
} else { | ||
n = 3 * n + 1; | ||
} | ||
seq.push(n); | ||
} | ||
return seq; | ||
} | ||
function digitalRoots(n) { | ||
if (!Number.isInteger(n) || n <= 0) { | ||
throw new Error("The number must be positive integer"); | ||
} | ||
if (n < 10) { | ||
return n; | ||
} | ||
do { | ||
let chars = n.toString().split(""); | ||
n = chars.reduce((prev, c) => prev += Number(c), 0); | ||
} while (n >= 10); | ||
return n; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -507,3 +595,6 @@ 0 && (module.exports = { | ||
Vector, | ||
collatz, | ||
digitalRoots, | ||
factors, | ||
fromBase, | ||
gcd, | ||
@@ -513,4 +604,5 @@ lcm, | ||
prime, | ||
toBase, | ||
totient | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@rgsoft/math", | ||
"version": "1.0.8", | ||
"version": "1.1.0", | ||
"description": "Yet another JS math library", | ||
@@ -27,3 +27,5 @@ "main": "./dist/index.js", | ||
"factors", | ||
"totient" | ||
"totient", | ||
"digital-roots", | ||
"number-bases" | ||
], | ||
@@ -30,0 +32,0 @@ "author": "Ricardo Miranda <rgmiranda@live.com.ar>", |
@@ -296,2 +296,64 @@ # math-js | ||
In the example, this means that `48` is `2^4 + 3^1`. | ||
In the example, this means that `48` is `2^4 + 3^1`. | ||
## Number Totient | ||
The `totient` function gets the [totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function) | ||
from a positive integer, that is, the number of prime numbers from 1 to `n-1` | ||
for a given integer `n`. | ||
```javascript | ||
console.log(totient(100)); | ||
``` | ||
will ouput `40`. | ||
## Collatz Sequence | ||
The `collatz` function gets the [collatz](https://en.wikipedia.org/wiki/Collatz_conjecture) | ||
sequence from a positive integer. | ||
```javascript | ||
console.log(collatz(5)); | ||
``` | ||
will ouput `[ 5, 16, 8, 4, 2, 1 ]`. | ||
Optionally, it accepts a second parameter that limits the maximum length of the | ||
resulting sequence. | ||
```javascript | ||
console.log(collatz(5, 5)); | ||
``` | ||
will ouput `[ 5, 16, 8, 4, 2 ]`. | ||
## Digital Roots | ||
The `digitalRoots` function gets the digital roots from a positive integer, that | ||
is, the sum of all its digits. | ||
```javascript | ||
console.log(digitalRoots(19)); | ||
``` | ||
will ouput `1`. | ||
## Converting to Other Bases | ||
The `toBase` function gets the string representation of a given **positive integer** | ||
in a certain base. | ||
```javascript | ||
console.log(toBase(255, 16), toBase(255, 2)); | ||
``` | ||
will ouput `FF 11111111`. | ||
The inverse process is achieved with the `fromBase` function. | ||
```javascript | ||
console.log(fromBase('FF', 16), fromBase('11111111', 2)); | ||
``` | ||
outputs `255 255`. |
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
99416
1372
359