@algorithm.ts/gcd 3.0.0-alpha.1 → 3.0.0-alpha.3
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/lib/cjs/{index.js → index.cjs} +0 -2
- package/lib/esm/{index.js → index.mjs} +0 -0
- package/package.json +15 -9
- package/src/euclidean.ts +70 -0
- package/src/gcd.ts +35 -0
- package/src/index.ts +2 -0
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@algorithm.ts/gcd",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.3",
|
|
4
4
|
"description": "GCD + Euclidean algorithm",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "guanghechen",
|
|
@@ -17,8 +17,11 @@
|
|
|
17
17
|
"gcd",
|
|
18
18
|
"extended euclidean algorithm"
|
|
19
19
|
],
|
|
20
|
-
"
|
|
21
|
-
"
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
"import": "./lib/esm/index.mjs",
|
|
23
|
+
"require": "./lib/cjs/index.cjs"
|
|
24
|
+
},
|
|
22
25
|
"types": "lib/types/index.d.ts",
|
|
23
26
|
"source": "src/index.ts",
|
|
24
27
|
"license": "MIT",
|
|
@@ -27,21 +30,24 @@
|
|
|
27
30
|
},
|
|
28
31
|
"files": [
|
|
29
32
|
"lib/",
|
|
30
|
-
"
|
|
31
|
-
"!lib/**/*.d.ts.map",
|
|
33
|
+
"src/",
|
|
32
34
|
"package.json",
|
|
33
35
|
"CHANGELOG.md",
|
|
34
36
|
"LICENSE",
|
|
35
37
|
"README.md"
|
|
36
38
|
],
|
|
37
39
|
"scripts": {
|
|
38
|
-
"build": "cross-env NODE_ENV=production rollup -c ../../rollup.config.
|
|
39
|
-
"prebuild": "rimraf lib/",
|
|
40
|
+
"build": "rimraf lib/ && cross-env NODE_ENV=production rollup -c ../../rollup.config.mjs",
|
|
40
41
|
"prepublishOnly": "cross-env ROLLUP_SHOULD_SOURCEMAP=false yarn build",
|
|
41
42
|
"test": "cross-env TS_NODE_FILES=true jest --config ../../jest.config.js --rootDir ."
|
|
42
43
|
},
|
|
43
44
|
"dependencies": {
|
|
44
|
-
"@algorithm.ts/constant": "^3.0.0-alpha.
|
|
45
|
+
"@algorithm.ts/constant": "^3.0.0-alpha.3"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"cross-env": "^7.0.3",
|
|
49
|
+
"rimraf": "^3.0.2",
|
|
50
|
+
"rollup": "^3.9.1"
|
|
45
51
|
},
|
|
46
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "d50b4725bde1b43474f490a29672db5aa1482ae3"
|
|
47
53
|
}
|
package/src/euclidean.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { BIGINT_ONE, BIGINT_ZERO } from '@algorithm.ts/constant'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Extended Euclidean algorithm for solving the smallest
|
|
5
|
+
* integer solution (|x| + |y| smallest) of the equation `Ax + By = gcd(x,y)`.
|
|
6
|
+
*
|
|
7
|
+
* @param a
|
|
8
|
+
* @param b
|
|
9
|
+
* @returns
|
|
10
|
+
* @see https://me.guanghechen.com/post/math/number-theory/%E6%A8%A1%E6%96%B9%E7%A8%8B/basic/
|
|
11
|
+
*/
|
|
12
|
+
export function euclidean(a: number, b: number): [number, number, number] {
|
|
13
|
+
let x: number
|
|
14
|
+
let y: number
|
|
15
|
+
let d: number
|
|
16
|
+
solve(a, b)
|
|
17
|
+
return [x!, y!, d!]
|
|
18
|
+
|
|
19
|
+
function solve(_a: number, _b: number): void {
|
|
20
|
+
if (_b === 0) {
|
|
21
|
+
x = 1
|
|
22
|
+
y = 0
|
|
23
|
+
d = _a
|
|
24
|
+
} else {
|
|
25
|
+
const A = _a % _b
|
|
26
|
+
const B = _b
|
|
27
|
+
solve(B, A)
|
|
28
|
+
|
|
29
|
+
const X = y
|
|
30
|
+
const Y = x
|
|
31
|
+
|
|
32
|
+
x = X
|
|
33
|
+
y = Y - Math.floor(_a / _b) * X
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Bigint version.
|
|
40
|
+
*
|
|
41
|
+
* @param a
|
|
42
|
+
* @param b
|
|
43
|
+
* @returns
|
|
44
|
+
* @see https://me.guanghechen.com/post/math/number-theory/%E6%A8%A1%E6%96%B9%E7%A8%8B/basic/
|
|
45
|
+
*/
|
|
46
|
+
export function euclideanBigint(a: bigint, b: bigint): [bigint, bigint, bigint] {
|
|
47
|
+
let x: bigint
|
|
48
|
+
let y: bigint
|
|
49
|
+
let d: bigint
|
|
50
|
+
solve(a, b)
|
|
51
|
+
return [x!, y!, d!]
|
|
52
|
+
|
|
53
|
+
function solve(_a: bigint, _b: bigint): void {
|
|
54
|
+
if (_b === BIGINT_ZERO) {
|
|
55
|
+
x = BIGINT_ONE
|
|
56
|
+
y = BIGINT_ZERO
|
|
57
|
+
d = _a
|
|
58
|
+
} else {
|
|
59
|
+
const A = _a % _b
|
|
60
|
+
const B = _b
|
|
61
|
+
solve(B, A)
|
|
62
|
+
|
|
63
|
+
const X = y
|
|
64
|
+
const Y = x
|
|
65
|
+
|
|
66
|
+
x = X
|
|
67
|
+
y = Y - (_a / _b) * X
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
package/src/gcd.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { BIGINT_ZERO } from '@algorithm.ts/constant'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Find the greatest common divisor of x and y
|
|
5
|
+
*
|
|
6
|
+
* @param x
|
|
7
|
+
* @param y
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export function gcd(x: number, y: number): number {
|
|
11
|
+
let i = x
|
|
12
|
+
for (let j = y; j !== 0; ) {
|
|
13
|
+
const t = i
|
|
14
|
+
i = j
|
|
15
|
+
j = t % i
|
|
16
|
+
}
|
|
17
|
+
return i
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Bigint version.
|
|
22
|
+
*
|
|
23
|
+
* @param x
|
|
24
|
+
* @param y
|
|
25
|
+
* @returns
|
|
26
|
+
*/
|
|
27
|
+
export function gcdBigint(x: bigint, y: bigint): bigint {
|
|
28
|
+
let i = x
|
|
29
|
+
for (let j = y; j !== BIGINT_ZERO; ) {
|
|
30
|
+
const t = i
|
|
31
|
+
i = j
|
|
32
|
+
j = t % i
|
|
33
|
+
}
|
|
34
|
+
return i
|
|
35
|
+
}
|
package/src/index.ts
ADDED