smath 2.0.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin.js +10 -0
- package/dist/smath.js +57 -4
- package/package.json +1 -1
- package/types/smath.d.ts +17 -0
package/dist/bin.js
CHANGED
|
@@ -24,6 +24,7 @@ if (func.includes('help')) {
|
|
|
24
24
|
console.log(' logspace <min> <max> <n> : Generate `n` logarithmically spaced numbers between `min` and `max`');
|
|
25
25
|
console.log(' factorial <n> : Compute `n!` (factorial)');
|
|
26
26
|
console.log(' factors <n> : List the prime factors of `n`');
|
|
27
|
+
console.log(' isPrime <n> : Determine if `n` is prime');
|
|
27
28
|
console.log(' round2 <n> <base> : Round `n` to a multiple of any `base`');
|
|
28
29
|
console.log(' error <exp> <act> : Calculate the normaized percent error between `exp` and `act`');
|
|
29
30
|
console.log(' sum <c0> [c1] ... [cn] : Compute a total of `n` numbers');
|
|
@@ -46,6 +47,7 @@ if (func.includes('help')) {
|
|
|
46
47
|
console.log(' : Select an index at random from a list of weights');
|
|
47
48
|
console.log(' rat <n> [eps] : Decompose `n` into a ratio');
|
|
48
49
|
console.log(' mixed <n> [eps] : Decompose `n` into a mixed number');
|
|
50
|
+
console.log(' gcd <a> <b> : Compute the greatest common denominator of two numbers');
|
|
49
51
|
process.exit(1);
|
|
50
52
|
}
|
|
51
53
|
switch (func) {
|
|
@@ -85,6 +87,10 @@ switch (func) {
|
|
|
85
87
|
console.log(SMath.factors(nums[0]));
|
|
86
88
|
break;
|
|
87
89
|
}
|
|
90
|
+
case ('isPrime'): {
|
|
91
|
+
console.log(SMath.isPrime(nums[0]));
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
88
94
|
case ('round2'): {
|
|
89
95
|
console.log(SMath.round2(nums[0], nums[1]));
|
|
90
96
|
break;
|
|
@@ -161,6 +167,10 @@ switch (func) {
|
|
|
161
167
|
console.log(SMath.mixed(nums[0], nums[1] ?? 1e-6));
|
|
162
168
|
break;
|
|
163
169
|
}
|
|
170
|
+
case ('gcd'): {
|
|
171
|
+
console.log(SMath.gcd(nums[0], nums[1]));
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
164
174
|
case (''): {
|
|
165
175
|
console.error('Missing argument. Use with "help" for a list of commands.');
|
|
166
176
|
process.exit(1);
|
package/dist/smath.js
CHANGED
|
@@ -110,7 +110,7 @@ export function logspace(min, max, count) {
|
|
|
110
110
|
* const y = SMath.factorial(5); // 120
|
|
111
111
|
*/
|
|
112
112
|
export function factorial(n) {
|
|
113
|
-
if (n < 0 ||
|
|
113
|
+
if (n < 0 || n % 1 !== 0) {
|
|
114
114
|
throw new Error('Input must be a positive integer.');
|
|
115
115
|
}
|
|
116
116
|
else if (n === 0) {
|
|
@@ -131,8 +131,8 @@ export function factorial(n) {
|
|
|
131
131
|
* const y = SMath.factors(12); // [ 2, 2, 3 ]
|
|
132
132
|
*/
|
|
133
133
|
export function factors(n) {
|
|
134
|
-
if (n < 0 ||
|
|
135
|
-
throw new Error('Input must be a positive integer
|
|
134
|
+
if (n < 0 || n % 1 !== 0) {
|
|
135
|
+
throw new Error('Input must be a positive integer.');
|
|
136
136
|
}
|
|
137
137
|
if (n <= 3) {
|
|
138
138
|
return [n];
|
|
@@ -140,7 +140,7 @@ export function factors(n) {
|
|
|
140
140
|
const f = [];
|
|
141
141
|
let i = 2;
|
|
142
142
|
while (n > 1 && i <= n) {
|
|
143
|
-
if (
|
|
143
|
+
if (n % i === 0) {
|
|
144
144
|
n /= i;
|
|
145
145
|
f.push(i);
|
|
146
146
|
}
|
|
@@ -150,6 +150,31 @@ export function factors(n) {
|
|
|
150
150
|
}
|
|
151
151
|
return f;
|
|
152
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* An optimized algorithm to determine if any number is prime.
|
|
155
|
+
* @param n Any positive integer
|
|
156
|
+
* @returns `true` if `n` is prime
|
|
157
|
+
* @example
|
|
158
|
+
* const b = SMath.isPrime(5); // true
|
|
159
|
+
*/
|
|
160
|
+
export function isPrime(n) {
|
|
161
|
+
if (n <= 1 || (n | 0) !== n) {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
if (n <= 3) {
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
if (n % 2 === 0 || n % 3 === 0) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
// Check 6x-1 and 6x+1 (all prime forms)
|
|
171
|
+
for (let i = 5; i * i <= n; i += 6) {
|
|
172
|
+
if (n % i === 0 || n % (i + 2) === 0) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
153
178
|
/**
|
|
154
179
|
* Round a number to the nearest multiple of an arbitrary
|
|
155
180
|
* base. Does not round when the base is set to zero.
|
|
@@ -504,3 +529,31 @@ export function rat(n, epsilon = 1e-6) {
|
|
|
504
529
|
export function mixed(n, epsilon = 1e-6) {
|
|
505
530
|
return { whole: n | 0, ...rat(n < -1 ? (n | 0) - n : n - (n | 0), epsilon) };
|
|
506
531
|
}
|
|
532
|
+
/**
|
|
533
|
+
* Get the greatest common denominator (GCD) of two numbers.
|
|
534
|
+
* @param a Any positive integer
|
|
535
|
+
* @param b Any positive integer
|
|
536
|
+
* @returns The GCD of two numbers
|
|
537
|
+
* @example
|
|
538
|
+
* const y = SMath.gcd(42, 30); // 6
|
|
539
|
+
*/
|
|
540
|
+
export function gcd(a, b) {
|
|
541
|
+
// Make sure input is valid
|
|
542
|
+
if (a < 0 || a % 1 !== 0 || b < 0 || b % 1 !== 0) {
|
|
543
|
+
throw new Error('Input must be a positive integer.');
|
|
544
|
+
}
|
|
545
|
+
// Reached the end
|
|
546
|
+
if (a === 0) {
|
|
547
|
+
return b;
|
|
548
|
+
}
|
|
549
|
+
else if (b === 0) {
|
|
550
|
+
return a;
|
|
551
|
+
}
|
|
552
|
+
// Compute the algorithm
|
|
553
|
+
if (a > b) {
|
|
554
|
+
return gcd(a % b, b);
|
|
555
|
+
}
|
|
556
|
+
else {
|
|
557
|
+
return gcd(a, b % a);
|
|
558
|
+
}
|
|
559
|
+
}
|
package/package.json
CHANGED
package/types/smath.d.ts
CHANGED
|
@@ -91,6 +91,14 @@ export declare function factorial(n: number): number;
|
|
|
91
91
|
* const y = SMath.factors(12); // [ 2, 2, 3 ]
|
|
92
92
|
*/
|
|
93
93
|
export declare function factors(n: number): number[];
|
|
94
|
+
/**
|
|
95
|
+
* An optimized algorithm to determine if any number is prime.
|
|
96
|
+
* @param n Any positive integer
|
|
97
|
+
* @returns `true` if `n` is prime
|
|
98
|
+
* @example
|
|
99
|
+
* const b = SMath.isPrime(5); // true
|
|
100
|
+
*/
|
|
101
|
+
export declare function isPrime(n: number): boolean;
|
|
94
102
|
/**
|
|
95
103
|
* Round a number to the nearest multiple of an arbitrary
|
|
96
104
|
* base. Does not round when the base is set to zero.
|
|
@@ -303,3 +311,12 @@ export declare function mixed(n: number, epsilon?: number): {
|
|
|
303
311
|
num: number;
|
|
304
312
|
den: number;
|
|
305
313
|
};
|
|
314
|
+
/**
|
|
315
|
+
* Get the greatest common denominator (GCD) of two numbers.
|
|
316
|
+
* @param a Any positive integer
|
|
317
|
+
* @param b Any positive integer
|
|
318
|
+
* @returns The GCD of two numbers
|
|
319
|
+
* @example
|
|
320
|
+
* const y = SMath.gcd(42, 30); // 6
|
|
321
|
+
*/
|
|
322
|
+
export declare function gcd(a: number, b: number): number;
|