From 7fefac0304aa9bfada7711df232a0175d802ec9f Mon Sep 17 00:00:00 2001 From: vishalt12345 Date: Sun, 26 Jun 2022 01:24:18 -0500 Subject: [PATCH 1/3] Added Recursive Extended Euclidean Algorithm --- src/algorithms/graph/centroid/centroid.js | 0 .../__test__/extendedEuclideanAlgorithmTest.js | 0 .../extendedeuclideanalgorithm.js | 7 +++++++ 3 files changed, 7 insertions(+) create mode 100644 src/algorithms/graph/centroid/centroid.js create mode 100644 src/algorithms/math/extended-euclidean-algorithm/__test__/extendedEuclideanAlgorithmTest.js create mode 100644 src/algorithms/math/extended-euclidean-algorithm/extendedeuclideanalgorithm.js diff --git a/src/algorithms/graph/centroid/centroid.js b/src/algorithms/graph/centroid/centroid.js new file mode 100644 index 00000000..e69de29b diff --git a/src/algorithms/math/extended-euclidean-algorithm/__test__/extendedEuclideanAlgorithmTest.js b/src/algorithms/math/extended-euclidean-algorithm/__test__/extendedEuclideanAlgorithmTest.js new file mode 100644 index 00000000..e69de29b diff --git a/src/algorithms/math/extended-euclidean-algorithm/extendedeuclideanalgorithm.js b/src/algorithms/math/extended-euclidean-algorithm/extendedeuclideanalgorithm.js new file mode 100644 index 00000000..08e9e94e --- /dev/null +++ b/src/algorithms/math/extended-euclidean-algorithm/extendedeuclideanalgorithm.js @@ -0,0 +1,7 @@ +export default function extendedEuclideanAlgorithm(a, b){ + if(b== 0){ + return [1, 0]; + } + var a = extendedEuclideanAlgorithm(b, a % b); + return [a[1], a[0] - Math.floor(a/b) * a[1]]; +} \ No newline at end of file From 3b30293c26810c3c4dfc8e15b7153f9008dc79b4 Mon Sep 17 00:00:00 2001 From: vishalt12345 <104476960+vishalt12345@users.noreply.github.com> Date: Sun, 26 Jun 2022 01:31:51 -0500 Subject: [PATCH 2/3] Create README.md --- src/algorithms/math/extended-euclidean-algorithm/README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/algorithms/math/extended-euclidean-algorithm/README.md diff --git a/src/algorithms/math/extended-euclidean-algorithm/README.md b/src/algorithms/math/extended-euclidean-algorithm/README.md new file mode 100644 index 00000000..4c60494e --- /dev/null +++ b/src/algorithms/math/extended-euclidean-algorithm/README.md @@ -0,0 +1,6 @@ +

Extended Euclidean Algorithm

+ +The standard Euclidean algorithm only calculates the gcd of a and b given 2 integers a and b. The extended Euclidean algorithm takes this one step further. For any 2 integers +a, b, the extended Euclidean algorithm calculates x and y such that ax + by = gcd(a, b). + + From 5ff34c240ef2773951bcdfe2e674370b876cf660 Mon Sep 17 00:00:00 2001 From: vishalt12345 <104476960+vishalt12345@users.noreply.github.com> Date: Sun, 26 Jun 2022 01:41:23 -0500 Subject: [PATCH 3/3] Update README.md --- .../math/extended-euclidean-algorithm/README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/algorithms/math/extended-euclidean-algorithm/README.md b/src/algorithms/math/extended-euclidean-algorithm/README.md index 4c60494e..62cce7a6 100644 --- a/src/algorithms/math/extended-euclidean-algorithm/README.md +++ b/src/algorithms/math/extended-euclidean-algorithm/README.md @@ -1,6 +1,19 @@

Extended Euclidean Algorithm

The standard Euclidean algorithm only calculates the gcd of a and b given 2 integers a and b. The extended Euclidean algorithm takes this one step further. For any 2 integers -a, b, the extended Euclidean algorithm calculates x and y such that ax + by = gcd(a, b). +a and b, the extended Euclidean algorithm calculates x and y such that ax + by = gcd(a, b). To perform this, the extended euclidean algorithm makes some slight modifications to the original euclidean algorithm. +The original euclidean algorithm arrived at a = gcd(a,b) and b = 0. Notice that if the original a and b equalled these values, then the solution would merely be x = 1 and y = 0. We can use these 2 values as a starting point for our algorithm. That way, all we would have to do is figure out how x and y change during the transition from (a, b) to (b, a mod b). +Let us say that we have x1 and y1 such that b * x1 + (a mod b) * y1 = gcd(a, b).
+ +Notice that we can substitute a mod b with a - floor(a/b) * b.
+ +Thus, after rearranging the terms, we get gcd(a, b) = a * y1 + b * (x1 - y1 * floor(a / b)).
+ +So after every step,
+ +x = y1
+y = x1 - y1 * floor(a/b).
+ +**We can use this to eventually backtrack and arrive at the desired x and y.**