From 1ae79a03963986e0899bb510bf7261e4dc367bea Mon Sep 17 00:00:00 2001 From: Kevin Duconge <64347790+kevduc@users.noreply.github.com> Date: Mon, 17 May 2021 08:05:17 +0100 Subject: [PATCH] Exporting gcd function --- docs/global.html | 351 +++++++++++++++++++++++++++++++++++++++++++-- docs/index.html | 4 +- docs/index.js.html | 41 ++++-- index.js | 37 +++-- package.json | 2 +- 5 files changed, 400 insertions(+), 35 deletions(-) diff --git a/docs/global.html b/docs/global.html index 9ca655e..cec95ce 100644 --- a/docs/global.html +++ b/docs/global.html @@ -24,7 +24,7 @@
@@ -308,7 +308,7 @@
Properties:
Source:
@@ -349,7 +349,7 @@

Methods

-

dioSolve(a, b, c) → {Result}

+

dioSolve(a, b, c) → {Solution}

@@ -538,7 +538,7 @@
Returns:
-Result +Solution
@@ -546,7 +546,193 @@
Returns:
- One object with all the result values. + An object with all the result values. +
+ + +
+ + + + + + +
+ + + +

gcd(a, b) → {GCD}

+ + + + + +
+ Finds the Greatest Common Divisor of a and b using the Euclidian algorithm +
+ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
a + + +number + + + + + Integer + +
b + + +number + + + + + Integer + +
+ + + + + + + + + + + + + + +
+
Returns:
+ + + +
+
+ Type: +
+
+ +GCD + + +
+
+ + +
+ An object with properties g (gcd value) and steps (steps of the Euclidian algorithm)
@@ -564,7 +750,156 @@

Type Definitions

-

Result

+

GCD

+ + + + + + + + +
Properties:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
g + + +number + + + + Value of GCD(a,b)
steps + + +Array + + + + Steps of the Euclidian algorithm, last step first: [[an, bn], ..., [a0, b0]]
+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + +
Type:
+
    +
  • + +Object + + +
  • +
+ + + + + +
+ + + +
+

Solution

@@ -768,7 +1103,7 @@
Properties:
Source:
@@ -813,7 +1148,7 @@
Type:

- Generated by JSDoc 3.6.6 on Fri May 14 2021 14:50:34 GMT+0100 (British Summer Time) using the Minami theme. + Generated by JSDoc 3.6.6 on Mon May 17 2021 08:03:00 GMT+0100 (British Summer Time) using the Minami theme.
diff --git a/docs/index.html b/docs/index.html index 0cfdc57..1959927 100644 --- a/docs/index.html +++ b/docs/index.html @@ -24,7 +24,7 @@
@@ -125,7 +125,7 @@

Support the dev


- Generated by JSDoc 3.6.6 on Fri May 14 2021 14:50:34 GMT+0100 (British Summer Time) using the Minami theme. + Generated by JSDoc 3.6.6 on Mon May 17 2021 08:03:01 GMT+0100 (British Summer Time) using the Minami theme.
diff --git a/docs/index.js.html b/docs/index.js.html index 36f4c2a..d1e602e 100644 --- a/docs/index.js.html +++ b/docs/index.js.html @@ -24,7 +24,7 @@
@@ -46,7 +46,7 @@

index.js

* @param {number} b Coefficient for <code>y</code> * @param {number} c Right-side constant * - * @returns {Result} One object with all the result values. + * @returns {Solution} An object with all the result values. */ const dioSolve = (a, b, c) => { @@ -60,8 +60,7 @@

index.js

const signs = [Math.sign(a), Math.sign(b)] ;[a, b] = [Math.abs(a), Math.abs(b)] - const steps = [] - const g = gcd(a, b, steps) + const { g, steps } = gcd(a, b) if (g === 0 && c === 0) return { ...defaultReturn, g, solutionType: 'always-true' } @@ -78,7 +77,7 @@

index.js

;[a, b, c] = [a / g, b / g, c / g] // Get quotients from gcd calculation steps - const q = steps.map(([a, b]) => Math.floor(a / b)).reverse() + const q = steps.map(([a, b]) => Math.floor(a / b)) let Ai1 = 0 let Ai0 = 1 @@ -114,17 +113,33 @@

index.js

} } -const gcd = (a, b, steps = []) => { - if (a === 0) return b - if (b === 0) return a +/** + * @typedef {Object} GCD + * @property {number} g Value of <code>GCD(a,b)</code> + * @property {Array} steps Steps of the Euclidian algorithm, last step first: <code>[[an, bn], ..., [a0, b0]]</code> + */ + +/** + * Finds the Greatest Common Divisor of <code>a</code> and <code>b</code> using the Euclidian algorithm + * + * @param {number} a Integer + * @param {number} b Integer + * + * @returns {GCD} An object with properties <code>g</code> (gcd value) and <code>steps</code> (steps of the Euclidian algorithm) + */ + +const gcd = (a, b) => { + if (a === 0) return { g: b, steps: [] } + if (b === 0) return { g: a, steps: [] } const r = a % b + if (r === 0) return { g: Math.abs(b), steps: [[a, b]] } + const { g, steps } = gcd(b, r) steps.push([a, b]) - if (r === 0) return Math.abs(b) - return gcd(b, r, steps) + return { g, steps } } /** - * @typedef {Object} Result + * @typedef {Object} Solution * @property {SolutionType} solutionType Type of solution found, see <code>{@link SolutionType}</code> for more information. * @property {number|null} g Value of <code>GCD(a,b)</code>, <code>null</code> if error. * @property {(Array|null)} z Initial solution <code>[x<sub>0</sub>, y<sub>0</sub>]</code> found using the Euclidean algorithm when solution is linear, <code>[x<sub>0</sub>, null]</code> or <code>[null, y<sub>0</sub>]</code> when solution is unique, else <code>null</code>. @@ -151,7 +166,7 @@

index.js

Error: 'error', } -module.exports = { dioSolve, SolutionType } +module.exports = { dioSolve, SolutionType, gcd } @@ -164,7 +179,7 @@

index.js


- Generated by JSDoc 3.6.6 on Fri May 14 2021 14:50:34 GMT+0100 (British Summer Time) using the Minami theme. + Generated by JSDoc 3.6.6 on Mon May 17 2021 08:03:00 GMT+0100 (British Summer Time) using the Minami theme.
diff --git a/index.js b/index.js index 7dd9127..f858452 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ * @param {number} b Coefficient for y * @param {number} c Right-side constant * - * @returns {Result} One object with all the result values. + * @returns {Solution} An object with all the result values. */ const dioSolve = (a, b, c) => { @@ -19,8 +19,7 @@ const dioSolve = (a, b, c) => { const signs = [Math.sign(a), Math.sign(b)] ;[a, b] = [Math.abs(a), Math.abs(b)] - const steps = [] - const g = gcd(a, b, steps) + const { g, steps } = gcd(a, b) if (g === 0 && c === 0) return { ...defaultReturn, g, solutionType: 'always-true' } @@ -37,7 +36,7 @@ const dioSolve = (a, b, c) => { ;[a, b, c] = [a / g, b / g, c / g] // Get quotients from gcd calculation steps - const q = steps.map(([a, b]) => Math.floor(a / b)).reverse() + const q = steps.map(([a, b]) => Math.floor(a / b)) let Ai1 = 0 let Ai0 = 1 @@ -73,17 +72,33 @@ const dioSolve = (a, b, c) => { } } -const gcd = (a, b, steps = []) => { - if (a === 0) return b - if (b === 0) return a +/** + * @typedef {Object} GCD + * @property {number} g Value of GCD(a,b) + * @property {Array} steps Steps of the Euclidian algorithm, last step first: [[an, bn], ..., [a0, b0]] + */ + +/** + * Finds the Greatest Common Divisor of a and b using the Euclidian algorithm + * + * @param {number} a Integer + * @param {number} b Integer + * + * @returns {GCD} An object with properties g (gcd value) and steps (steps of the Euclidian algorithm) + */ + +const gcd = (a, b) => { + if (a === 0) return { g: b, steps: [] } + if (b === 0) return { g: a, steps: [] } const r = a % b + if (r === 0) return { g: Math.abs(b), steps: [[a, b]] } + const { g, steps } = gcd(b, r) steps.push([a, b]) - if (r === 0) return Math.abs(b) - return gcd(b, r, steps) + return { g, steps } } /** - * @typedef {Object} Result + * @typedef {Object} Solution * @property {SolutionType} solutionType Type of solution found, see {@link SolutionType} for more information. * @property {number|null} g Value of GCD(a,b), null if error. * @property {(Array|null)} z Initial solution [x0, y0] found using the Euclidean algorithm when solution is linear, [x0, null] or [null, y0] when solution is unique, else null. @@ -110,4 +125,4 @@ const SolutionType = { Error: 'error', } -module.exports = { dioSolve, SolutionType } +module.exports = { dioSolve, SolutionType, gcd } diff --git a/package.json b/package.json index f3213b9..b3f5da0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "diophantine", "description": "Solve linear diophantine equations", - "version": "1.0.6", + "version": "1.1.0", "author": "kevduc", "license": "ISC", "main": "index.js",