@algorithm.ts/gcd 2.0.12 → 3.0.0-alpha.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/README.md +3 -9
- package/lib/cjs/index.js +25 -9
- package/lib/esm/index.js +25 -9
- package/lib/types/index.d.ts +20 -17
- package/package.json +8 -5
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<header>
|
|
2
2
|
<h1 align="center">
|
|
3
|
-
<a href="https://github.com/guanghechen/algorithm.ts/tree/
|
|
3
|
+
<a href="https://github.com/guanghechen/algorithm.ts/tree/release-3.x.x/packages/gcd#readme">@algorithm.ts/gcd</a>
|
|
4
4
|
</h1>
|
|
5
5
|
<div align="center">
|
|
6
6
|
<a href="https://www.npmjs.com/package/@algorithm.ts/gcd">
|
|
@@ -72,15 +72,9 @@ A typescript implementation of the **gcd** and **Extended Euclidean** algorithm.
|
|
|
72
72
|
yarn add @algorithm.ts/gcd
|
|
73
73
|
```
|
|
74
74
|
|
|
75
|
-
* deno
|
|
76
|
-
|
|
77
|
-
```typescript
|
|
78
|
-
import { gcd, euclidean } from 'https://raw.githubusercontent.com/guanghechen/algorithm.ts/main/packages/gcd/src/index.ts'
|
|
79
|
-
```
|
|
80
75
|
|
|
81
76
|
## Usage
|
|
82
77
|
|
|
83
|
-
|
|
84
78
|
* gcd
|
|
85
79
|
|
|
86
80
|
```typescript
|
|
@@ -107,5 +101,5 @@ A typescript implementation of the **gcd** and **Extended Euclidean** algorithm.
|
|
|
107
101
|
* [数论基础之模方程初步][gcd]
|
|
108
102
|
|
|
109
103
|
|
|
110
|
-
[homepage]: https://github.com/guanghechen/algorithm.ts/tree/
|
|
111
|
-
[gcd]: https://me.guanghechen.com/post/math/number-theory/%E6%A8%A1%E6%96%B9%E7%A8%8B/basic/
|
|
104
|
+
[homepage]: https://github.com/guanghechen/algorithm.ts/tree/release-3.x.x/packages/gcd#readme
|
|
105
|
+
[gcd]: https://me.guanghechen.com/post/math/number-theory/%E6%A8%A1%E6%96%B9%E7%A8%8B/basic/
|
package/lib/cjs/index.js
CHANGED
|
@@ -2,12 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
function gcdBigint(x, y) {
|
|
9
|
-
return y === 0n ? x : gcdBigint(y, x % y);
|
|
10
|
-
}
|
|
5
|
+
const BIGINT_ZERO = BigInt(0);
|
|
6
|
+
const BIGINT_ONE = BigInt(1);
|
|
7
|
+
|
|
11
8
|
function euclidean(a, b) {
|
|
12
9
|
let x;
|
|
13
10
|
let y;
|
|
@@ -38,9 +35,9 @@ function euclideanBigint(a, b) {
|
|
|
38
35
|
solve(a, b);
|
|
39
36
|
return [x, y, d];
|
|
40
37
|
function solve(_a, _b) {
|
|
41
|
-
if (_b ===
|
|
42
|
-
x =
|
|
43
|
-
y =
|
|
38
|
+
if (_b === BIGINT_ZERO) {
|
|
39
|
+
x = BIGINT_ONE;
|
|
40
|
+
y = BIGINT_ZERO;
|
|
44
41
|
d = _a;
|
|
45
42
|
}
|
|
46
43
|
else {
|
|
@@ -55,6 +52,25 @@ function euclideanBigint(a, b) {
|
|
|
55
52
|
}
|
|
56
53
|
}
|
|
57
54
|
|
|
55
|
+
function gcd(x, y) {
|
|
56
|
+
let i = x;
|
|
57
|
+
for (let j = y; j !== 0;) {
|
|
58
|
+
const t = i;
|
|
59
|
+
i = j;
|
|
60
|
+
j = t % i;
|
|
61
|
+
}
|
|
62
|
+
return i;
|
|
63
|
+
}
|
|
64
|
+
function gcdBigint(x, y) {
|
|
65
|
+
let i = x;
|
|
66
|
+
for (let j = y; j !== BIGINT_ZERO;) {
|
|
67
|
+
const t = i;
|
|
68
|
+
i = j;
|
|
69
|
+
j = t % i;
|
|
70
|
+
}
|
|
71
|
+
return i;
|
|
72
|
+
}
|
|
73
|
+
|
|
58
74
|
exports.euclidean = euclidean;
|
|
59
75
|
exports.euclideanBigint = euclideanBigint;
|
|
60
76
|
exports.gcd = gcd;
|
package/lib/esm/index.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
function gcdBigint(x, y) {
|
|
5
|
-
return y === 0n ? x : gcdBigint(y, x % y);
|
|
6
|
-
}
|
|
1
|
+
const BIGINT_ZERO = BigInt(0);
|
|
2
|
+
const BIGINT_ONE = BigInt(1);
|
|
3
|
+
|
|
7
4
|
function euclidean(a, b) {
|
|
8
5
|
let x;
|
|
9
6
|
let y;
|
|
@@ -34,9 +31,9 @@ function euclideanBigint(a, b) {
|
|
|
34
31
|
solve(a, b);
|
|
35
32
|
return [x, y, d];
|
|
36
33
|
function solve(_a, _b) {
|
|
37
|
-
if (_b ===
|
|
38
|
-
x =
|
|
39
|
-
y =
|
|
34
|
+
if (_b === BIGINT_ZERO) {
|
|
35
|
+
x = BIGINT_ONE;
|
|
36
|
+
y = BIGINT_ZERO;
|
|
40
37
|
d = _a;
|
|
41
38
|
}
|
|
42
39
|
else {
|
|
@@ -51,4 +48,23 @@ function euclideanBigint(a, b) {
|
|
|
51
48
|
}
|
|
52
49
|
}
|
|
53
50
|
|
|
51
|
+
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;
|
|
59
|
+
}
|
|
60
|
+
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;
|
|
68
|
+
}
|
|
69
|
+
|
|
54
70
|
export { euclidean, euclideanBigint, gcd, gcdBigint };
|
package/lib/types/index.d.ts
CHANGED
|
@@ -1,35 +1,38 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Extended Euclidean algorithm for solving the smallest
|
|
3
|
+
* integer solution (|x| + |y| smallest) of the equation `Ax + By = gcd(x,y)`.
|
|
3
4
|
*
|
|
4
|
-
* @param
|
|
5
|
-
* @param
|
|
5
|
+
* @param a
|
|
6
|
+
* @param b
|
|
6
7
|
* @returns
|
|
8
|
+
* @see https://me.guanghechen.com/post/math/number-theory/%E6%A8%A1%E6%96%B9%E7%A8%8B/basic/
|
|
7
9
|
*/
|
|
8
|
-
|
|
10
|
+
declare function euclidean(a: number, b: number): [number, number, number];
|
|
9
11
|
/**
|
|
10
12
|
* Bigint version.
|
|
11
13
|
*
|
|
12
|
-
* @param
|
|
13
|
-
* @param
|
|
14
|
+
* @param a
|
|
15
|
+
* @param b
|
|
14
16
|
* @returns
|
|
17
|
+
* @see https://me.guanghechen.com/post/math/number-theory/%E6%A8%A1%E6%96%B9%E7%A8%8B/basic/
|
|
15
18
|
*/
|
|
16
|
-
|
|
19
|
+
declare function euclideanBigint(a: bigint, b: bigint): [bigint, bigint, bigint];
|
|
20
|
+
|
|
17
21
|
/**
|
|
18
|
-
*
|
|
19
|
-
* integer solution (|x| + |y| smallest) of the equation `Ax + By = gcd(x,y)`.
|
|
22
|
+
* Find the greatest common divisor of x and y
|
|
20
23
|
*
|
|
21
|
-
* @param
|
|
22
|
-
* @param
|
|
24
|
+
* @param x
|
|
25
|
+
* @param y
|
|
23
26
|
* @returns
|
|
24
|
-
* @see https://me.guanghechen.com/post/math/number-theory/%E6%A8%A1%E6%96%B9%E7%A8%8B/basic/
|
|
25
27
|
*/
|
|
26
|
-
|
|
28
|
+
declare function gcd(x: number, y: number): number;
|
|
27
29
|
/**
|
|
28
30
|
* Bigint version.
|
|
29
31
|
*
|
|
30
|
-
* @param
|
|
31
|
-
* @param
|
|
32
|
+
* @param x
|
|
33
|
+
* @param y
|
|
32
34
|
* @returns
|
|
33
|
-
* @see https://me.guanghechen.com/post/math/number-theory/%E6%A8%A1%E6%96%B9%E7%A8%8B/basic/
|
|
34
35
|
*/
|
|
35
|
-
|
|
36
|
+
declare function gcdBigint(x: bigint, y: bigint): bigint;
|
|
37
|
+
|
|
38
|
+
export { euclidean, euclideanBigint, gcd, gcdBigint };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@algorithm.ts/gcd",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-alpha.0",
|
|
4
4
|
"description": "GCD + Euclidean algorithm",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "guanghechen",
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "https://github.com/guanghechen/algorithm.ts.
|
|
11
|
+
"url": "https://github.com/guanghechen/algorithm.ts/tree/release-3.x.x",
|
|
12
12
|
"directory": "packages/gcd"
|
|
13
13
|
},
|
|
14
|
-
"homepage": "https://github.com/guanghechen/algorithm.ts/tree/
|
|
14
|
+
"homepage": "https://github.com/guanghechen/algorithm.ts/tree/release-3.x.x/packages/gcd#readme",
|
|
15
15
|
"keywords": [
|
|
16
16
|
"algorithm",
|
|
17
17
|
"gcd",
|
|
@@ -36,9 +36,12 @@
|
|
|
36
36
|
],
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "cross-env NODE_ENV=production rollup -c ../../rollup.config.js",
|
|
39
|
-
"prebuild": "rimraf lib/
|
|
39
|
+
"prebuild": "rimraf lib/",
|
|
40
40
|
"prepublishOnly": "cross-env ROLLUP_SHOULD_SOURCEMAP=false yarn build",
|
|
41
41
|
"test": "cross-env TS_NODE_FILES=true jest --config ../../jest.config.js --rootDir ."
|
|
42
42
|
},
|
|
43
|
-
"
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@algorithm.ts/constant": "^3.0.0-alpha.0"
|
|
45
|
+
},
|
|
46
|
+
"gitHead": "7562a908843d63b6b1bf92e7aa2104e7b294eaa0"
|
|
44
47
|
}
|