@algorithm.ts/gcd 3.0.0-alpha.2 → 3.0.0-alpha.4
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} +1 -0
- package/lib/cjs/index.cjs.map +1 -0
- package/lib/esm/{index.js → index.mjs} +1 -0
- package/lib/esm/index.mjs.map +1 -0
- package/package.json +22 -13
- package/src/euclidean.ts +70 -0
- package/src/gcd.ts +35 -0
- package/src/index.ts +2 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../../constant/lib/esm/index.mjs","../../src/euclidean.ts","../../src/gcd.ts"],"sourcesContent":["const BIGINT_ZERO = BigInt(0);\nconst BIGINT_ONE = BigInt(1);\nconst CODEPOINT_DIGIT_0 = '0'.codePointAt(0);\nconst CODEPOINT_DIGIT_9 = '9'.codePointAt(0);\nconst CODEPOINT_UPPER_A = 'A'.codePointAt(0);\nconst CODEPOINT_UPPER_Z = 'Z'.codePointAt(0);\nconst CODEPOINT_LOWER_A = 'a'.codePointAt(0);\nconst CODEPOINT_LOWER_Z = 'z'.codePointAt(0);\n\nexport { BIGINT_ONE, BIGINT_ZERO, CODEPOINT_DIGIT_0, CODEPOINT_DIGIT_9, CODEPOINT_LOWER_A, CODEPOINT_LOWER_Z, CODEPOINT_UPPER_A, CODEPOINT_UPPER_Z };\n//# sourceMappingURL=index.mjs.map\n",null,null],"names":[],"mappings":";;AAAA,MAAM,WAAW,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;AAC9B,MAAM,UAAU,GAAG,MAAM,CAAC,CAAC,CAAC;;ACUZ,SAAA,SAAS,CAAC,CAAS,EAAE,CAAS,EAAA;AAC5C,IAAA,IAAI,CAAS,CAAA;AACb,IAAA,IAAI,CAAS,CAAA;AACb,IAAA,IAAI,CAAS,CAAA;AACb,IAAA,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;AACX,IAAA,OAAO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AM,CAAC,GAAG,CAAC,CAAA;YACX,MAAM,CAAC,GAAG,CAAC,CAAA;YAEX,CAAC,GAAG,CAAC,CAAA;YACL,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,CAAA;AACtB,SAAA;KACF;AACH;;AC5DgB,SAAA,GAAG,CAAC,CAAS,EAAE,CAAS,EAAA;IACtC,IAAI,CAAC,GAAG,CAAC,CAAA;IACT,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,KAAK,CAAC,GAAI;QACzB,MAAM,CAAC,GAAG,CAAC,CAAA;QACX,CAAC,GAAG,CAAC,CAAA;AACL,QAAA,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;AACV,KAAA;AACD,IAAA,OAAO,CAAC,CAAA;AACV,CAAC;AASe,SAAA,SAAS,CAAC,CAAS,EAAE,CAAS,EAAA;IAC5C,IAAI,CAAC,GAAG,CAAC,CAAA;IACT,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,KAAK,WAAW,GAAI;QACnC,MAAM,CAAC,GAAG,CAAC,CAAA;QACX,CAAC,GAAG,CAAC,CAAA;AACL,QAAA,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;AACV,KAAA;AACD,IAAA,OAAO,CAAC,CAAA;AACV;;;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../../constant/lib/esm/index.mjs","../../src/euclidean.ts","../../src/gcd.ts"],"sourcesContent":["const BIGINT_ZERO = BigInt(0);\nconst BIGINT_ONE = BigInt(1);\nconst CODEPOINT_DIGIT_0 = '0'.codePointAt(0);\nconst CODEPOINT_DIGIT_9 = '9'.codePointAt(0);\nconst CODEPOINT_UPPER_A = 'A'.codePointAt(0);\nconst CODEPOINT_UPPER_Z = 'Z'.codePointAt(0);\nconst CODEPOINT_LOWER_A = 'a'.codePointAt(0);\nconst CODEPOINT_LOWER_Z = 'z'.codePointAt(0);\n\nexport {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,CAAC,GAAG,CAAC,CAAA;AACV,KAAA;AACD,IAAA,OAAO,CAAC,CAAA;AACV;;;;"}
|
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.4",
|
|
4
4
|
"description": "GCD + Euclidean algorithm",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "guanghechen",
|
|
@@ -17,31 +17,40 @@
|
|
|
17
17
|
"gcd",
|
|
18
18
|
"extended euclidean algorithm"
|
|
19
19
|
],
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
"types": "./lib/types/index.d.ts",
|
|
23
|
+
"require": "./lib/cjs/index.cjs",
|
|
24
|
+
"import": "./lib/esm/index.mjs"
|
|
25
|
+
},
|
|
26
|
+
"types": "./lib/types/index.d.ts",
|
|
27
|
+
"main": "./lib/cjs/index.cjs",
|
|
28
|
+
"module": "./lib/esm/index.mjs",
|
|
29
|
+
"source": "./src/index.ts",
|
|
24
30
|
"license": "MIT",
|
|
25
31
|
"engines": {
|
|
26
32
|
"node": ">= 14.15.0"
|
|
27
33
|
},
|
|
28
34
|
"files": [
|
|
29
35
|
"lib/",
|
|
30
|
-
"
|
|
31
|
-
"!lib/**/*.d.ts.map",
|
|
36
|
+
"src/",
|
|
32
37
|
"package.json",
|
|
33
38
|
"CHANGELOG.md",
|
|
34
39
|
"LICENSE",
|
|
35
40
|
"README.md"
|
|
36
41
|
],
|
|
37
42
|
"scripts": {
|
|
38
|
-
"build": "cross-env NODE_ENV=production rollup -c ../../rollup.config.mjs",
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"test": "cross-env TS_NODE_FILES=true jest --config ../../jest.config.js --rootDir ."
|
|
43
|
+
"build": "rimraf lib/ && cross-env NODE_ENV=production rollup -c ../../rollup.config.mjs",
|
|
44
|
+
"prepublishOnly": "yarn build",
|
|
45
|
+
"test": "cross-env TS_NODE_FILES=true NODE_OPTIONS=--experimental-vm-modules jest --config ../../jest.config.mjs --rootDir ."
|
|
42
46
|
},
|
|
43
47
|
"dependencies": {
|
|
44
|
-
"@algorithm.ts/constant": "^3.0.0-alpha.
|
|
48
|
+
"@algorithm.ts/constant": "^3.0.0-alpha.4"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"cross-env": "^7.0.3",
|
|
52
|
+
"rimraf": "^3.0.2",
|
|
53
|
+
"rollup": "^3.9.1"
|
|
45
54
|
},
|
|
46
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "969d68502c3c35690ed7aee86dde2fe60cddddfc"
|
|
47
56
|
}
|
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