You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/algebra/euclid-algorithm.md
+7-25Lines changed: 7 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,9 @@ The algorithm was first described in Euclid's "Elements" (circa 300 BC), but it
20
20
21
21
## Algorithm
22
22
23
-
The algorithm is extremely simple:
23
+
Originally, the Euclidean algorithm was formulated as follows: subtract the smaller number from the larger one until one of the numbers is zero. Indeed, if $g$ divides $a$ and $b$, it also divides $a-b$. On the other hand, if $g$ divides $a-b$ and $b$, then it also divides $a = b + (a-b)$, which means that the sets of the common divisors of $\{a, b\}$ and $\{b,a-b\}$ coincide.
24
+
25
+
Note that $a$ remains the larger number until $b$ is subtracted from it at least $\left\lfloor\frac{a}{b}\right\rfloor$ times. Therefore, to speed things up, $a-b$ is substituted with $a-\left\lfloor\frac{a}{b}\right\rfloor b = a \bmod b$. Then the algorithm is formulated in an extremely simple way:
24
26
25
27
$$\gcd(a, b) = \begin{cases}a,&\text{if }b = 0 \\ \gcd(b, a \bmod b),&\text{otherwise.}\end{cases}$$
26
28
@@ -55,29 +57,7 @@ int gcd (int a, int b) {
55
57
}
56
58
```
57
59
58
-
## Correctness Proof
59
-
60
-
First, notice that in each iteration of the Euclidean algorithm the second argument strictly decreases, therefore (since the arguments are always non-negative) the algorithm will always terminate.
61
-
62
-
For the proof of correctness, we need to show that $\gcd(a, b) = \gcd(b, a \bmod b)$ for all $a \geq 0$, $b > 0$.
63
-
64
-
We will show that the value on the left side of the equation divides the value on the right side and vice versa. Obviously, this would mean that the left and right sides are equal, which will prove Euclid's algorithm.
65
-
66
-
Let $d = \gcd(a, b)$. Then by definition $d\mid a$ and $d\mid b$.
67
-
68
-
Now let's represent the remainder of the division of $a$ by $b$ as follows:
69
-
70
-
$$a \bmod b = a - b \cdot \Bigl\lfloor\dfrac{a}{b}\Bigr\rfloor$$
71
-
72
-
From this it follows that $d \mid (a \bmod b)$, which means we have the system of divisibilities:
73
-
74
-
$$\begin{cases}d \mid b,\\ d \mid (a \mod b)\end{cases}$$
75
-
76
-
Now we use the fact that for any three numbers $p$, $q$, $r$, if $p\mid q$ and $p\mid r$ then $p\mid \gcd(q, r)$. In our case, we get:
77
-
78
-
$$d = \gcd(a, b) \mid \gcd(b, a \mod b)$$
79
-
80
-
Thus we have shown that the left side of the original equation divides the right. The second half of the proof is similar.
60
+
Note that since C++17, `gcd` is implemented as a [standard function](https://en.cppreference.com/w/cpp/numeric/gcd) in C++.
81
61
82
62
## Time Complexity
83
63
@@ -89,6 +69,8 @@ Moreover, it is possible to show that the upper bound of this theorem is optimal
89
69
90
70
Given that Fibonacci numbers grow exponentially, we get that the Euclidean algorithm works in $O(\log \min(a, b))$.
91
71
72
+
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.
73
+
92
74
## Least common multiple
93
75
94
76
Calculating the least common multiple (commonly denoted **LCM**) can be reduced to calculating the GCD with the following simple formula:
@@ -117,7 +99,7 @@ It's based on a few properties:
117
99
118
100
- If both numbers are even, then we can factor out a two of both and compute the GCD of the remaining numbers: $\gcd(2a, 2b) = 2 \gcd(a, b)$.
119
101
- If one of the numbers is even and the other one is odd, then we can remove the factor 2 from the even one: $\gcd(2a, b) = \gcd(a, b)$ if $b$ is odd.
120
-
- If both numbers are odd, then subtracting one number of the other one will not change the GCD: $\gcd(a, b) = \gcd(b, a-b)$ (this can be proven in the same way as the correctness proof of the normal algorithm)
102
+
- If both numbers are odd, then subtracting one number of the other one will not change the GCD: $\gcd(a, b) = \gcd(b, a-b)$
121
103
122
104
Using only these properties, and some fast bitwise functions from GCC, we can implement a fast version:
Copy file name to clipboardExpand all lines: src/algebra/linear-diophantine-equation.md
+27-1Lines changed: 27 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,33 @@ In this article, we consider several classical problems on these equations:
22
22
23
23
A degenerate case that need to be taken care of is when $a = b = 0$. It is easy to see that we either have no solutions or infinitely many solutions, depending on whether $c = 0$ or not. In the rest of this article, we will ignore this case.
24
24
25
-
## Finding a solution
25
+
## Analytic solution
26
+
27
+
When $a \neq 0$ and $b \neq 0$, the equation $ax+by=c$ can be equivalently treated as either of the following:
28
+
29
+
\begin{gather}
30
+
ax \equiv c \pmod b,\newline
31
+
by \equiv c \pmod a.
32
+
\end{gather}
33
+
34
+
Without loss of generality, assume that $b \neq 0$ and consider the first equation. When $a$ and $b$ are co-prime, the solution to it is given as
35
+
36
+
$$x \equiv ca^{-1} \pmod b,$$
37
+
38
+
where $a^{-1}$ is the [modular inverse](module-inverse.md) of $a$ modulo $b$.
39
+
40
+
When $a$ and $b$ are not co-prime, values of $ax$ modulo $b$ for all integer $x$ are divisible by $g=\gcd(a, b)$, so the solution only exists when $c$ is divisible by $g$. In this case, one of solutions can be found by reducing the equation by $g$:
41
+
42
+
$$(a/g) x \equiv (c/g) \pmod{b/g}.$$
43
+
44
+
By the definition of $g$, the numbers $a/g$ and $b/g$ are co-prime, so the solution is given explicitly as
45
+
46
+
$$\begin{cases}
47
+
x \equiv (c/g)(a/g)^{-1}\pmod{b/g},\\
48
+
y = \frac{c-ax}{b}.
49
+
\end{cases}$$
50
+
51
+
## Algorithmic solution
26
52
27
53
To find one solution of the Diophantine equation with 2 unknowns, you can use the [Extended Euclidean algorithm](extended-euclid-algorithm.md). First, assume that $a$ and $b$ are non-negative. When we apply Extended Euclidean algorithm for $a$ and $b$, we can find their greatest common divisor $g$ and 2 numbers $x_g$ and $y_g$ such that:
0 commit comments