From cd8d7789e1a8542d3e1cf6258d815391a793a1d4 Mon Sep 17 00:00:00 2001 From: Pedro <69079299+mdeapedro@users.noreply.github.com> Date: Sun, 13 Oct 2024 13:17:48 -0300 Subject: [PATCH 1/3] Update euclid-algorithm.md Add 'GCD of multiple numbers' section. --- src/algebra/euclid-algorithm.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/algebra/euclid-algorithm.md b/src/algebra/euclid-algorithm.md index 665ca2b07..4c3af00cf 100644 --- a/src/algebra/euclid-algorithm.md +++ b/src/algebra/euclid-algorithm.md @@ -88,6 +88,30 @@ int lcm (int a, int b) { } ``` +## GCD of multiple numbers + +Given an array of more than two integers, your task is to find the GCD of these integers, which is the largest number that divides **all** of them. + +A simple way to achieve this is to calculate the GCD of each integer with the previous GCD result: + +```cpp +// a[0..n-1] +int res = a[0]; +for (int i = 1; i < n; ++i) { + res = gcd(res, a[i]); +} +``` + +It's also possible to interrupt the loop earlier by checking if `res` is equal to 1: + +```cpp +int res = a[0]; +for (int i = 1; i < n; ++i) { + if (res == 1) break; + res = gcd(res, a[i]); +} +``` + ## Binary GCD The Binary GCD algorithm is an optimization to the normal Euclidean algorithm. From 1124c358ba3f7c429520a1115389d0ff2ace9576 Mon Sep 17 00:00:00 2001 From: Pedro Date: Sun, 13 Oct 2024 18:51:23 -0300 Subject: [PATCH 2/3] Update euclid-algorithm.md --- src/algebra/euclid-algorithm.md | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/src/algebra/euclid-algorithm.md b/src/algebra/euclid-algorithm.md index 4c3af00cf..2b7b6ca3c 100644 --- a/src/algebra/euclid-algorithm.md +++ b/src/algebra/euclid-algorithm.md @@ -15,7 +15,7 @@ $$\gcd(a, b) = \max \{k > 0 : (k \mid a) \text{ and } (k \mid b) \}$$ When one of the numbers is zero, while the other is non-zero, their greatest common divisor, by definition, is the second number. When both numbers are zero, their greatest common divisor is undefined (it can be any arbitrarily large number), but it is convenient to define it as zero as well to preserve the associativity of $\gcd$. Which gives us a simple rule: if one of the numbers is zero, the greatest common divisor is the other number. -The Euclidean algorithm, discussed below, allows to find the greatest common divisor of two numbers $a$ and $b$ in $O(\log \min(a, b))$. +The Euclidean algorithm, discussed below, allows to find the greatest common divisor of two numbers $a$ and $b$ in $O(\log \min(a, b))$. Since the function is **associative**, to find the GCD of **more than two numbers**, we can do $\gcd(a, b, c) = \gcd(a, \gcd(b, c))$ and so forth. The algorithm was first described in Euclid's "Elements" (circa 300 BC), but it is possible that the algorithm has even earlier origins. @@ -88,30 +88,6 @@ int lcm (int a, int b) { } ``` -## GCD of multiple numbers - -Given an array of more than two integers, your task is to find the GCD of these integers, which is the largest number that divides **all** of them. - -A simple way to achieve this is to calculate the GCD of each integer with the previous GCD result: - -```cpp -// a[0..n-1] -int res = a[0]; -for (int i = 1; i < n; ++i) { - res = gcd(res, a[i]); -} -``` - -It's also possible to interrupt the loop earlier by checking if `res` is equal to 1: - -```cpp -int res = a[0]; -for (int i = 1; i < n; ++i) { - if (res == 1) break; - res = gcd(res, a[i]); -} -``` - ## Binary GCD The Binary GCD algorithm is an optimization to the normal Euclidean algorithm. From cbb7b39eb5d0fb295c7bef9ef54f3ef207adc7dd Mon Sep 17 00:00:00 2001 From: Oleksandr Kulkov Date: Tue, 15 Oct 2024 12:01:03 +0200 Subject: [PATCH 3/3] Update euclid-algorithm.md --- src/algebra/euclid-algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algebra/euclid-algorithm.md b/src/algebra/euclid-algorithm.md index 2b7b6ca3c..2de931871 100644 --- a/src/algebra/euclid-algorithm.md +++ b/src/algebra/euclid-algorithm.md @@ -70,7 +70,7 @@ Moreover, it is possible to show that the upper bound of this theorem is optimal Given that Fibonacci numbers grow exponentially, we get that the Euclidean algorithm works in $O(\log \min(a, b))$. -Another way to estimate the complexity is to notice that $a \bmod b$ for the case $a \geq b$ is at least $2$ times smaller than $a$, so the larger number is reduced at least in half on each iteration of the algorithm. +Another way to estimate the complexity is to notice that $a \bmod b$ for the case $a \geq b$ is at least $2$ times smaller than $a$, so the larger number is reduced at least in half on each iteration of the algorithm. Applying this reasoning to the case when we compute the GCD of the set of numbers $a_1,\dots,a_n \leq C$, this also allows us to estimate the total runtime as $O(n + \log C)$, rather than $O(n \log C)$, since every non-trivial iteration of the algorithm reduces the current GCD candidate by at least a factor of $2$. ## Least common multiple