Skip to content

Commit 012042c

Browse files
MaxJWeinsteinjiegillet
authored andcommitted
euclid in MATLAB (#273)
Added into chapter's code list, edited markdown, and added name to contributors
1 parent c0ce24b commit 012042c

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

CONTRIBUTORS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ GuyPozner
4040
<br>
4141
William Boyles
4242
<br>
43+
Max Weinstein
44+
<br>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Submitted by Max Weinstein
2+
3+
function gcd = euclidSub(a,b)
4+
5+
a = abs(a);
6+
b = abs(b);
7+
8+
while a ~= b
9+
if a > b
10+
a = a - b;
11+
else
12+
b = b - a;
13+
end
14+
end
15+
16+
gcd = a;
17+
end
18+
19+
function gcd = euclidMod(a,b)
20+
21+
a=abs(a);
22+
b=abs(b);
23+
24+
while b > 0
25+
temp = b;
26+
b = mod(a,b);
27+
a = temp;
28+
end
29+
30+
gcd = a;
31+
end
32+
33+
function euclid()
34+
['gcd(520,420) via euclidSub: ',num2str(euclidSub(520,420))]
35+
['gcd(183,244) via euclidMod: ',num2str(euclidMod(183,244))]
36+
end

chapters/algorithms/euclidean_algorithm/euclidean_algorithm.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
3030
{% sample lang="swift" %}
3131
[import:1-15, lang="swift"](code/swift/euclidean_algorithm.swift)
3232
{% endmethod %}
33+
{% sample lang="matlab" %}
34+
[import:3-17, lang="matlab"](code/matlab/euclidean.m)
35+
{% endmethod %}
3336

3437
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:
3538

@@ -64,6 +67,8 @@ Modern implementations, though, often use the modulus operator (%) like so
6467
[import:14-23, lang="golang"](code/go/euclidean.go)
6568
{% sample lang="swift" %}
6669
[import:17-29, lang="swift"](code/swift/euclidean_algorithm.swift)
70+
{% sample lang="matlab" %}
71+
[import:19-31, lang="matlab"](code/matlab/euclidean.m)
6772
{% endmethod %}
6873

6974
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:
@@ -104,6 +109,8 @@ Program.cs
104109
[import, lang="golang"](code/go/euclidean.go)
105110
{% sample lang="swift" %}
106111
[import, lang="swift"](code/swift/euclidean_algorithm.swift)
112+
{% sample lang="matlab" %}
113+
[import, lang="matlab"](code/matlab/euclidean.m)
107114
{% endmethod %}
108115

109116

0 commit comments

Comments
 (0)