@algorithm.ts/gcd 4.0.5 → 4.0.6

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change Log
2
2
 
3
+ ## 4.0.6
4
+
5
+ ### Patch Changes
6
+
7
+ - Return correct Bézout coefficients for mixed-sign integer inputs to `euclidean` while preserving the sign of the returned divisor.
8
+
3
9
  ## 4.0.5
4
10
 
5
11
  ### Patch Changes
package/README.md CHANGED
@@ -39,10 +39,10 @@
39
39
  src="https://img.shields.io/badge/tested_with-vitest-6E9F18.svg"
40
40
  />
41
41
  </a>
42
- <a href="https://github.com/prettier/prettier">
42
+ <a href="https://biomejs.dev/">
43
43
  <img
44
- alt="Code Style: prettier"
45
- src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"
44
+ alt="Code Style: Biome"
45
+ src="https://img.shields.io/badge/code_style-Biome-60a5fa.svg?style=flat-square"
46
46
  />
47
47
  </a>
48
48
  </div>
package/lib/cjs/index.cjs CHANGED
@@ -1,74 +1,78 @@
1
- 'use strict';
2
-
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region ../_internal/lib/esm/index.mjs
3
3
  const BIGINT_ZERO = BigInt(0);
4
4
  const BIGINT_ONE = BigInt(1);
5
-
5
+ //#endregion
6
+ //#region src/euclidean.ts
6
7
  function euclidean(a, b) {
7
- let x;
8
- let y;
9
- let d;
10
- solve(a, b);
11
- return [x, y, d];
12
- function solve(_a, _b) {
13
- if (_b === 0) {
14
- x = 1;
15
- y = 0;
16
- d = _a;
17
- }
18
- else {
19
- const A = _a % _b;
20
- const B = _b;
21
- solve(B, A);
22
- const X = y;
23
- const Y = x;
24
- x = X;
25
- y = Y - Math.floor(_a / _b) * X;
26
- }
27
- }
8
+ let x;
9
+ let y;
10
+ let d;
11
+ solve(a, b);
12
+ return [
13
+ x,
14
+ y,
15
+ d
16
+ ];
17
+ function solve(_a, _b) {
18
+ if (_b === 0) {
19
+ x = 1;
20
+ y = 0;
21
+ d = _a;
22
+ } else {
23
+ solve(_b, _a % _b);
24
+ const X = y;
25
+ const Y = x;
26
+ x = X;
27
+ y = Y - Math.trunc(_a / _b) * X;
28
+ }
29
+ }
28
30
  }
29
31
  function euclideanBigint(a, b) {
30
- let x;
31
- let y;
32
- let d;
33
- solve(a, b);
34
- return [x, y, d];
35
- function solve(_a, _b) {
36
- if (_b === BIGINT_ZERO) {
37
- x = BIGINT_ONE;
38
- y = BIGINT_ZERO;
39
- d = _a;
40
- }
41
- else {
42
- const A = _a % _b;
43
- const B = _b;
44
- solve(B, A);
45
- const X = y;
46
- const Y = x;
47
- x = X;
48
- y = Y - (_a / _b) * X;
49
- }
50
- }
32
+ let x;
33
+ let y;
34
+ let d;
35
+ solve(a, b);
36
+ return [
37
+ x,
38
+ y,
39
+ d
40
+ ];
41
+ function solve(_a, _b) {
42
+ if (_b === BIGINT_ZERO) {
43
+ x = BIGINT_ONE;
44
+ y = BIGINT_ZERO;
45
+ d = _a;
46
+ } else {
47
+ solve(_b, _a % _b);
48
+ const X = y;
49
+ const Y = x;
50
+ x = X;
51
+ y = Y - _a / _b * X;
52
+ }
53
+ }
51
54
  }
52
-
55
+ //#endregion
56
+ //#region src/gcd.ts
53
57
  function gcd(x, y) {
54
- let i = x;
55
- for (let j = y; j !== 0;) {
56
- const t = i;
57
- i = j;
58
- j = t % i;
59
- }
60
- return i;
58
+ let i = x;
59
+ for (let j = y; j !== 0;) {
60
+ const t = i;
61
+ i = j;
62
+ j = t % i;
63
+ }
64
+ return i;
61
65
  }
62
66
  function gcdBigint(x, y) {
63
- let i = x;
64
- for (let j = y; j !== BIGINT_ZERO;) {
65
- const t = i;
66
- i = j;
67
- j = t % i;
68
- }
69
- return i;
67
+ let i = x;
68
+ for (let j = y; j !== BIGINT_ZERO;) {
69
+ const t = i;
70
+ i = j;
71
+ j = t % i;
72
+ }
73
+ return i;
70
74
  }
71
-
75
+ //#endregion
72
76
  exports.euclidean = euclidean;
73
77
  exports.euclideanBigint = euclideanBigint;
74
78
  exports.gcd = gcd;
package/lib/esm/index.mjs CHANGED
@@ -1,70 +1,75 @@
1
+ //#region ../_internal/lib/esm/index.mjs
1
2
  const BIGINT_ZERO = BigInt(0);
2
3
  const BIGINT_ONE = BigInt(1);
3
-
4
+ //#endregion
5
+ //#region src/euclidean.ts
4
6
  function euclidean(a, b) {
5
- let x;
6
- let y;
7
- let d;
8
- solve(a, b);
9
- return [x, y, d];
10
- function solve(_a, _b) {
11
- if (_b === 0) {
12
- x = 1;
13
- y = 0;
14
- d = _a;
15
- }
16
- else {
17
- const A = _a % _b;
18
- const B = _b;
19
- solve(B, A);
20
- const X = y;
21
- const Y = x;
22
- x = X;
23
- y = Y - Math.floor(_a / _b) * X;
24
- }
25
- }
7
+ let x;
8
+ let y;
9
+ let d;
10
+ solve(a, b);
11
+ return [
12
+ x,
13
+ y,
14
+ d
15
+ ];
16
+ function solve(_a, _b) {
17
+ if (_b === 0) {
18
+ x = 1;
19
+ y = 0;
20
+ d = _a;
21
+ } else {
22
+ solve(_b, _a % _b);
23
+ const X = y;
24
+ const Y = x;
25
+ x = X;
26
+ y = Y - Math.trunc(_a / _b) * X;
27
+ }
28
+ }
26
29
  }
27
30
  function euclideanBigint(a, b) {
28
- let x;
29
- let y;
30
- let d;
31
- solve(a, b);
32
- return [x, y, d];
33
- function solve(_a, _b) {
34
- if (_b === BIGINT_ZERO) {
35
- x = BIGINT_ONE;
36
- y = BIGINT_ZERO;
37
- d = _a;
38
- }
39
- else {
40
- const A = _a % _b;
41
- const B = _b;
42
- solve(B, A);
43
- const X = y;
44
- const Y = x;
45
- x = X;
46
- y = Y - (_a / _b) * X;
47
- }
48
- }
31
+ let x;
32
+ let y;
33
+ let d;
34
+ solve(a, b);
35
+ return [
36
+ x,
37
+ y,
38
+ d
39
+ ];
40
+ function solve(_a, _b) {
41
+ if (_b === BIGINT_ZERO) {
42
+ x = BIGINT_ONE;
43
+ y = BIGINT_ZERO;
44
+ d = _a;
45
+ } else {
46
+ solve(_b, _a % _b);
47
+ const X = y;
48
+ const Y = x;
49
+ x = X;
50
+ y = Y - _a / _b * X;
51
+ }
52
+ }
49
53
  }
50
-
54
+ //#endregion
55
+ //#region src/gcd.ts
51
56
  function gcd(x, y) {
52
- let i = x;
53
- for (let j = y; j !== 0;) {
54
- const t = i;
55
- i = j;
56
- j = t % i;
57
- }
58
- return i;
57
+ let i = x;
58
+ for (let j = y; j !== 0;) {
59
+ const t = i;
60
+ i = j;
61
+ j = t % i;
62
+ }
63
+ return i;
59
64
  }
60
65
  function gcdBigint(x, y) {
61
- let i = x;
62
- for (let j = y; j !== BIGINT_ZERO;) {
63
- const t = i;
64
- i = j;
65
- j = t % i;
66
- }
67
- return i;
66
+ let i = x;
67
+ for (let j = y; j !== BIGINT_ZERO;) {
68
+ const t = i;
69
+ i = j;
70
+ j = t % i;
71
+ }
72
+ return i;
68
73
  }
69
-
74
+ //#endregion
70
75
  export { euclidean, euclideanBigint, gcd, gcdBigint };
@@ -1,3 +1,4 @@
1
+ //#region src/euclidean.d.ts
1
2
  /**
2
3
  * Extended Euclidean algorithm for solving the smallest
3
4
  * integer solution (|x| + |y| smallest) of the equation `Ax + By = gcd(x,y)`.
@@ -7,7 +8,7 @@
7
8
  * @returns
8
9
  * @see https://me.guanghechen.com/post/math/number-theory/%E6%A8%A1%E6%96%B9%E7%A8%8B/basic/
9
10
  */
10
- declare function euclidean(a: number, b: number): [number, number, number];
11
+ export declare function euclidean(a: number, b: number): [number, number, number];
11
12
  /**
12
13
  * Bigint version.
13
14
  *
@@ -16,8 +17,9 @@ declare function euclidean(a: number, b: number): [number, number, number];
16
17
  * @returns
17
18
  * @see https://me.guanghechen.com/post/math/number-theory/%E6%A8%A1%E6%96%B9%E7%A8%8B/basic/
18
19
  */
19
- declare function euclideanBigint(a: bigint, b: bigint): [bigint, bigint, bigint];
20
-
20
+ export declare function euclideanBigint(a: bigint, b: bigint): [bigint, bigint, bigint];
21
+ //#endregion
22
+ //#region src/gcd.d.ts
21
23
  /**
22
24
  * Find the greatest common divisor of x and y
23
25
  *
@@ -25,7 +27,7 @@ declare function euclideanBigint(a: bigint, b: bigint): [bigint, bigint, bigint]
25
27
  * @param y
26
28
  * @returns
27
29
  */
28
- declare function gcd(x: number, y: number): number;
30
+ export declare function gcd(x: number, y: number): number;
29
31
  /**
30
32
  * Bigint version.
31
33
  *
@@ -33,6 +35,6 @@ declare function gcd(x: number, y: number): number;
33
35
  * @param y
34
36
  * @returns
35
37
  */
36
- declare function gcdBigint(x: bigint, y: bigint): bigint;
37
-
38
- export { euclidean, euclideanBigint, gcd, gcdBigint };
38
+ export declare function gcdBigint(x: bigint, y: bigint): bigint;
39
+ //#endregion
40
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@algorithm.ts/gcd",
3
- "version": "4.0.5",
3
+ "version": "4.0.6",
4
4
  "description": "GCD + Euclidean algorithm",
5
5
  "author": {
6
6
  "name": "guanghechen",
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "scripts": {
43
43
  "clean": "rimraf lib",
44
- "build": "rollup -c ../../rollup.config.mjs",
44
+ "build": "tsdown --config ../../tsdown.config.mjs",
45
45
  "test": "vitest run --config ../../vitest.config.ts",
46
46
  "test:coverage": "vitest run --config ../../vitest.config.ts --coverage",
47
47
  "test:update": "vitest run --config ../../vitest.config.ts -u"