From 0a658304c7219cece4338fa645037cb8ff333a6d Mon Sep 17 00:00:00 2001 From: Max Weinstein Date: Thu, 19 Jul 2018 00:52:43 -0400 Subject: [PATCH] euclid in MATLAB Added into chapter's code list, edited markdown, and added name to contributors --- CONTRIBUTORS.md | 2 ++ .../code/matlab/euclidean.m | 36 +++++++++++++++++++ .../euclidean_algorithm.md | 7 ++++ 3 files changed, 45 insertions(+) create mode 100644 chapters/algorithms/euclidean_algorithm/code/matlab/euclidean.m diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 50199d0d4..2a2de28b9 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -40,3 +40,5 @@ GuyPozner
William Boyles
+Max Weinstein +
diff --git a/chapters/algorithms/euclidean_algorithm/code/matlab/euclidean.m b/chapters/algorithms/euclidean_algorithm/code/matlab/euclidean.m new file mode 100644 index 000000000..de2a63dec --- /dev/null +++ b/chapters/algorithms/euclidean_algorithm/code/matlab/euclidean.m @@ -0,0 +1,36 @@ +// Submitted by Max Weinstein + +function gcd = euclidSub(a,b) + + a = abs(a); + b = abs(b); + + while a ~= b + if a > b + a = a - b; + else + b = b - a; + end + end + + gcd = a; +end + +function gcd = euclidMod(a,b) + + a=abs(a); + b=abs(b); + + while b > 0 + temp = b; + b = mod(a,b); + a = temp; + end + + gcd = a; +end + +function euclid() + ['gcd(520,420) via euclidSub: ',num2str(euclidSub(520,420))] + ['gcd(183,244) via euclidMod: ',num2str(euclidMod(183,244))] +end \ No newline at end of file diff --git a/chapters/algorithms/euclidean_algorithm/euclidean_algorithm.md b/chapters/algorithms/euclidean_algorithm/euclidean_algorithm.md index 84da2d483..53150d636 100644 --- a/chapters/algorithms/euclidean_algorithm/euclidean_algorithm.md +++ b/chapters/algorithms/euclidean_algorithm/euclidean_algorithm.md @@ -30,6 +30,9 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two {% sample lang="swift" %} [import:1-15, lang="swift"](code/swift/euclidean_algorithm.swift) {% endmethod %} +{% sample lang="matlab" %} +[import:3-17, lang="matlab"](code/matlab/euclidean.m) +{% endmethod %} 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: @@ -64,6 +67,8 @@ Modern implementations, though, often use the modulus operator (%) like so [import:14-23, lang="golang"](code/go/euclidean.go) {% sample lang="swift" %} [import:17-29, lang="swift"](code/swift/euclidean_algorithm.swift) +{% sample lang="matlab" %} +[import:19-31, lang="matlab"](code/matlab/euclidean.m) {% endmethod %} 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 [import, lang="golang"](code/go/euclidean.go) {% sample lang="swift" %} [import, lang="swift"](code/swift/euclidean_algorithm.swift) +{% sample lang="matlab" %} +[import, lang="matlab"](code/matlab/euclidean.m) {% endmethod %}