@algorithm.ts/gcd 2.0.14 → 3.0.0-alpha.1

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020-2021 guanghechen (me.guanghechen@gmail.com)
3
+ Copyright (c) 2020-2022 guanghechen (me.guanghechen@gmail.com)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
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/release-2.x.x/packages/gcd#readme">@algorithm.ts/gcd</a>
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/release-2.x.x/packages/gcd#readme
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
- function gcd(x, y) {
6
- return y === 0 ? x : gcd(y, x % y);
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 === 0n) {
42
- x = 1n;
43
- y = 0n;
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
- function gcd(x, y) {
2
- return y === 0 ? x : gcd(y, x % y);
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 === 0n) {
38
- x = 1n;
39
- y = 0n;
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 };
@@ -1,19 +1,3 @@
1
- /**
2
- * Find the greatest common divisor of x and y
3
- *
4
- * @param x
5
- * @param y
6
- * @returns
7
- */
8
- declare function gcd(x: number, y: number): number;
9
- /**
10
- * Bigint version.
11
- *
12
- * @param x
13
- * @param y
14
- * @returns
15
- */
16
- declare function gcdBigint(x: bigint, y: bigint): bigint;
17
1
  /**
18
2
  * Extended Euclidean algorithm for solving the smallest
19
3
  * integer solution (|x| + |y| smallest) of the equation `Ax + By = gcd(x,y)`.
@@ -34,4 +18,21 @@ declare function euclidean(a: number, b: number): [number, number, number];
34
18
  */
35
19
  declare function euclideanBigint(a: bigint, b: bigint): [bigint, bigint, bigint];
36
20
 
21
+ /**
22
+ * Find the greatest common divisor of x and y
23
+ *
24
+ * @param x
25
+ * @param y
26
+ * @returns
27
+ */
28
+ declare function gcd(x: number, y: number): number;
29
+ /**
30
+ * Bigint version.
31
+ *
32
+ * @param x
33
+ * @param y
34
+ * @returns
35
+ */
36
+ declare function gcdBigint(x: bigint, y: bigint): bigint;
37
+
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": "2.0.14",
3
+ "version": "3.0.0-alpha.1",
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/tree/release-2.x.x",
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/release-2.x.x/packages/gcd#readme",
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",
@@ -40,5 +40,8 @@
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
- "gitHead": "53a24624195c9f09422c9769c552f9066bc22c70"
43
+ "dependencies": {
44
+ "@algorithm.ts/constant": "^3.0.0-alpha.1"
45
+ },
46
+ "gitHead": "8f9a0d0dfee15efc08c4165848123b118c13ce81"
44
47
  }