|
23 | 23 |
|
24 | 24 | # Euclidean Algorithm
|
25 | 25 |
|
| 26 | +Computer science is (almost by definition) a science about computers -- a device first conceptualized in the 1800's. Computers have become so revolutionary, that it is difficult to think of our lives today without them. That said, *algorithms* are much older and have existed in the world for millenia. Incredibly, a few of the algorithms created before the Common Era (AD) are still in use today. One such algorithm was first described in Euclid's *Elements* (~ 300 BC) and has come to be known as the *Euclidean Algorithm*. |
| 27 | + |
| 28 | +The algorithm is a simple way to find the *greatest common divisor* (GCD) of two numbers, which is useful for a number of different applications (like reducing fractions). The first method (envisioned by Euclid) uses simple subtraction: |
| 29 | + |
| 30 | +```python |
| 31 | +function euclid_sub(a::Int64, b::Int64) |
| 32 | + while (a != b) |
| 33 | + if (a > b) |
| 34 | + a = a - b |
| 35 | + else |
| 36 | + b = b - a |
| 37 | + end |
| 38 | + end |
| 39 | +end |
| 40 | +``` |
| 41 | + |
| 42 | +Here, we simply line the two numbers up every step and subtract the lower value from the higher one every timestep. Once the two values are equal, we call that value the greatest common divisor. A graph of `a` and `b` as they change every step would look something like this: |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +Modern implementations, though, often use the modulus operator (%) like so |
| 47 | + |
| 48 | +```python |
| 49 | +function euclid_mod(a::Int64, b::Int64) |
| 50 | + temp = Int64 |
| 51 | + while (b != 0) |
| 52 | + temp = b |
| 53 | + b = a%b |
| 54 | + a = temp |
| 55 | + end |
| 56 | +end |
| 57 | +``` |
| 58 | + |
| 59 | +Here, we set `b` to be the remainder of `a%b` and `a` to be whatever `b` was last timestep. Because of how the modulus operator works, this will provide the same information as the subtraction-based implementation, but when we show `a` and `b` as they change with time, we can see that it might take many fewer steps: |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +The Euclidean Algorithm is truly fundamental to many other algorithms throughout the history of computer science and will definitely be used again later. At least to me, it's amazing how such an ancient algorithm can still have modern use and appeal. That said, there are still other algorithms out there that can find the greatest common divisor of two numbers that are arguably better in certain cases than the Euclidean algorithm, but the fact that we are discussing Euclid two millenia after his death shows how timeless and universal mathematics truly is. I think that's pretty cool. |
| 64 | + |
| 65 | +# Example Code |
| 66 | +### C++ |
| 67 | +```cpp |
| 68 | +/*-------------euclidean.cpp--------------------------------------------------// |
| 69 | +* |
| 70 | +* Purpose: To implement euclidean algorithm to find the greatest common divisor |
| 71 | +* |
| 72 | +* Notes: Compile with g++ euclidean.cpp |
| 73 | +* |
| 74 | +*-----------------------------------------------------------------------------*/ |
| 75 | + |
| 76 | +#include <iostream> |
| 77 | +#include <math.h> |
| 78 | + |
| 79 | +// Euclidean algorithm with mod |
| 80 | +int euclid_mod(int a, int b){ |
| 81 | + |
| 82 | + int temp; |
| 83 | + while (b != 0){ |
| 84 | + temp = b; |
| 85 | + b = a%b; |
| 86 | + a = temp; |
| 87 | + } |
| 88 | + |
| 89 | + return a; |
| 90 | +} |
| 91 | + |
| 92 | +// Euclidean algorithm with subtraction |
| 93 | +int euclid_sub(int a, int b){ |
| 94 | + |
| 95 | + while (a != b){ |
| 96 | + if (a > b){ |
| 97 | + a = a - b; |
| 98 | + } |
| 99 | + else{ |
| 100 | + b = b - a; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return a; |
| 105 | +} |
| 106 | + |
| 107 | +int main(){ |
| 108 | + |
| 109 | + int check = euclid_mod(64*67, 64*81); |
| 110 | + int check2 = euclid_sub(128*12, 128*77); |
| 111 | + |
| 112 | + std::cout << check << '\n'; |
| 113 | + std::cout << check2 << '\n'; |
| 114 | +} |
| 115 | + |
| 116 | +``` |
0 commit comments