@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 +6 -0
- package/README.md +3 -3
- package/lib/cjs/index.cjs +65 -61
- package/lib/esm/index.mjs +64 -59
- package/lib/types/index.d.ts +9 -7
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
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://
|
|
42
|
+
<a href="https://biomejs.dev/">
|
|
43
43
|
<img
|
|
44
|
-
alt="Code Style:
|
|
45
|
-
src="https://img.shields.io/badge/code_style-
|
|
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
|
-
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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 };
|
package/lib/types/index.d.ts
CHANGED
|
@@ -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 {
|
|
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.
|
|
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": "
|
|
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"
|