smath 2.1.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 CHANGED
@@ -47,6 +47,7 @@ if (func.includes('help')) {
47
47
  console.log(' : Select an index at random from a list of weights');
48
48
  console.log(' rat <n> [eps] : Decompose `n` into a ratio');
49
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');
50
51
  process.exit(1);
51
52
  }
52
53
  switch (func) {
@@ -166,6 +167,10 @@ switch (func) {
166
167
  console.log(SMath.mixed(nums[0], nums[1] ?? 1e-6));
167
168
  break;
168
169
  }
170
+ case ('gcd'): {
171
+ console.log(SMath.gcd(nums[0], nums[1]));
172
+ break;
173
+ }
169
174
  case (''): {
170
175
  console.error('Missing argument. Use with "help" for a list of commands.');
171
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 || (n | 0) !== n) {
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 || (n | 0) !== n) {
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 ((n / i) === ((n / i) | 0)) {
143
+ if (n % i === 0) {
144
144
  n /= i;
145
145
  f.push(i);
146
146
  }
@@ -154,6 +154,8 @@ export function factors(n) {
154
154
  * An optimized algorithm to determine if any number is prime.
155
155
  * @param n Any positive integer
156
156
  * @returns `true` if `n` is prime
157
+ * @example
158
+ * const b = SMath.isPrime(5); // true
157
159
  */
158
160
  export function isPrime(n) {
159
161
  if (n <= 1 || (n | 0) !== n) {
@@ -527,3 +529,31 @@ export function rat(n, epsilon = 1e-6) {
527
529
  export function mixed(n, epsilon = 1e-6) {
528
530
  return { whole: n | 0, ...rat(n < -1 ? (n | 0) - n : n - (n | 0), epsilon) };
529
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smath",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Small math function library",
5
5
  "homepage": "https://npm.nicfv.com/",
6
6
  "type": "module",
package/types/smath.d.ts CHANGED
@@ -95,6 +95,8 @@ export declare function factors(n: number): number[];
95
95
  * An optimized algorithm to determine if any number is prime.
96
96
  * @param n Any positive integer
97
97
  * @returns `true` if `n` is prime
98
+ * @example
99
+ * const b = SMath.isPrime(5); // true
98
100
  */
99
101
  export declare function isPrime(n: number): boolean;
100
102
  /**
@@ -309,3 +311,12 @@ export declare function mixed(n: number, epsilon?: number): {
309
311
  num: number;
310
312
  den: number;
311
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;