Skip to content

Commit 298be35

Browse files
committed
adding euclidean algorithm
1 parent 7d97574 commit 298be35

File tree

4 files changed

+96
-5
lines changed

4 files changed

+96
-5
lines changed

chapters/fundamental_algorithms/euclidean_algorithm/euclidean.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,94 @@ $$
2323

2424
# Euclidean Algorithm
2525

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+
![Subtraction-based Euclidean algorithm](subtraction.png)
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+
![Modulus-based Euclidean algorithm](modulus.png)
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+
```
Loading
Loading

chapters/fundamental_algorithms/tree_traversal.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def main():
328328
main()
329329
```
330330

331-
#### Python 2
331+
### Python 2
332332
```python
333333
# Depth-First and Breadth-First Traversal
334334
# Submitted by Matthew Giallourakis
@@ -578,7 +578,7 @@ int main() {
578578
}
579579
```
580580
581-
#### C#:
581+
### C#:
582582
583583
```cs
584584
// submitted by Julian Schacher‏
@@ -686,7 +686,7 @@ namespace Tree_Traversal
686686
}
687687
```
688688

689-
#### JavaScript
689+
### JavaScript
690690

691691
```html
692692
<!DOCTYPE html>
@@ -761,7 +761,7 @@ BFS_queue(root);
761761
<html>
762762
```
763763

764-
#### Haskell
764+
### Haskell
765765
```hs
766766
--- Submitted by Jie
767767
data Tree a = Empty
@@ -794,7 +794,7 @@ testTree = Node 1 [Node 2 [Node 3 [],
794794

795795
```
796796

797-
#### Scratch
797+
### Scratch
798798
Submitted by Jie
799799

800800
[https://scratch.mit.edu/projects/174017753/](https://scratch.mit.edu/projects/174017753/)

0 commit comments

Comments
 (0)