smath 2.0.0 → 2.1.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 +5 -0
- package/dist/smath.js +23 -0
- package/package.json +1 -1
- package/types/smath.d.ts +6 -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');
|
|
@@ -85,6 +86,10 @@ switch (func) {
|
|
|
85
86
|
console.log(SMath.factors(nums[0]));
|
|
86
87
|
break;
|
|
87
88
|
}
|
|
89
|
+
case ('isPrime'): {
|
|
90
|
+
console.log(SMath.isPrime(nums[0]));
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
88
93
|
case ('round2'): {
|
|
89
94
|
console.log(SMath.round2(nums[0], nums[1]));
|
|
90
95
|
break;
|
package/dist/smath.js
CHANGED
|
@@ -150,6 +150,29 @@ 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
|
+
*/
|
|
158
|
+
export function isPrime(n) {
|
|
159
|
+
if (n <= 1 || (n | 0) !== n) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
if (n <= 3) {
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
if (n % 2 === 0 || n % 3 === 0) {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
// Check 6x-1 and 6x+1 (all prime forms)
|
|
169
|
+
for (let i = 5; i * i <= n; i += 6) {
|
|
170
|
+
if (n % i === 0 || n % (i + 2) === 0) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
153
176
|
/**
|
|
154
177
|
* Round a number to the nearest multiple of an arbitrary
|
|
155
178
|
* base. Does not round when the base is set to zero.
|
package/package.json
CHANGED
package/types/smath.d.ts
CHANGED
|
@@ -91,6 +91,12 @@ 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
|
+
*/
|
|
99
|
+
export declare function isPrime(n: number): boolean;
|
|
94
100
|
/**
|
|
95
101
|
* Round a number to the nearest multiple of an arbitrary
|
|
96
102
|
* base. Does not round when the base is set to zero.
|