From ca787738407c6396d9e5759657da46a7a14cfae2 Mon Sep 17 00:00:00 2001 From: CD Sigma Date: Sun, 22 Jul 2018 18:55:19 -0700 Subject: [PATCH 1/3] Adding Euclidean Algorithm in Lua --- book.json | 4 +++ .../code/lua/euclidean.lua | 34 +++++++++++++++++++ .../euclidean_algorithm.md | 6 ++++ 3 files changed, 44 insertions(+) create mode 100644 contents/euclidean_algorithm/code/lua/euclidean.lua diff --git a/book.json b/book.json index ceb0d60b0..87f2728a1 100644 --- a/book.json +++ b/book.json @@ -116,6 +116,10 @@ { "lang": "ti83b", "name": "TI-83 Basic" + }, + { + "lang": "lua", + "name": "Lua" } ] } diff --git a/contents/euclidean_algorithm/code/lua/euclidean.lua b/contents/euclidean_algorithm/code/lua/euclidean.lua new file mode 100644 index 000000000..935c25328 --- /dev/null +++ b/contents/euclidean_algorithm/code/lua/euclidean.lua @@ -0,0 +1,34 @@ +function euclidSub (a, b) + local a = math.abs(a) + local b = math.abs(b) + + while a ~= b do + if a > b then + a = a-b + else + b = b-a + end + end + + return a +end + +function euclidMod (a, b) + local a = math.abs(a) + local b = math.abs(b) + + while b ~= 0 do + local temp = b + b = a % b + a = temp + end + + return a +end + +function main () + print(euclidSub(128 * 12, 128 * 77)) + print(euclidMod(64 * 67, 64 * 81)) +end + +main() diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 707a04236..20b94d887 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -31,6 +31,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two [import:1-15, lang="swift"](code/swift/euclidean_algorithm.swift) {% sample lang="matlab" %} [import:3-17, lang="matlab"](code/matlab/euclidean.m) +{% sample lang="lua" %} +[import:1-14, lang="lua"](code/lua/euclidean.lua) {% 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: @@ -68,6 +70,8 @@ Modern implementations, though, often use the modulus operator (%) like so [import:17-29, lang="swift"](code/swift/euclidean_algorithm.swift) {% sample lang="matlab" %} [import:19-31, lang="matlab"](code/matlab/euclidean.m) +{% sample lang="lua" %} +[import:16-27, lang="lua"](code/lua/euclidean.lua) {% 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: @@ -110,6 +114,8 @@ Program.cs [import, lang="swift"](code/swift/euclidean_algorithm.swift) {% sample lang="matlab" %} [import, lang="matlab"](code/matlab/euclidean.m) +{% sample lang="lua" %} +[import, lang="lua"](code/lua/euclidean.lua) {% endmethod %} From 5fc0fd700144ffda9c6121338023f949d05f9731 Mon Sep 17 00:00:00 2001 From: CD Sigma Date: Tue, 24 Jul 2018 16:33:13 -0700 Subject: [PATCH 2/3] Assigning pairwise variables --- contents/euclidean_algorithm/code/lua/euclidean.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/contents/euclidean_algorithm/code/lua/euclidean.lua b/contents/euclidean_algorithm/code/lua/euclidean.lua index 935c25328..45ef1fd18 100644 --- a/contents/euclidean_algorithm/code/lua/euclidean.lua +++ b/contents/euclidean_algorithm/code/lua/euclidean.lua @@ -18,9 +18,7 @@ function euclidMod (a, b) local b = math.abs(b) while b ~= 0 do - local temp = b - b = a % b - a = temp + a, b = b, a%b end return a From e0d9c75e1d397fb309c45b81ec86632b6882ab83 Mon Sep 17 00:00:00 2001 From: CDsigma Date: Fri, 27 Jul 2018 14:27:04 -0700 Subject: [PATCH 3/3] Fixing Code Cutoffs for Lua in euclid's .md File --- contents/euclidean_algorithm/euclidean_algorithm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 20b94d887..5529493d9 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -71,7 +71,7 @@ Modern implementations, though, often use the modulus operator (%) like so {% sample lang="matlab" %} [import:19-31, lang="matlab"](code/matlab/euclidean.m) {% sample lang="lua" %} -[import:16-27, lang="lua"](code/lua/euclidean.lua) +[import:16-25, lang="lua"](code/lua/euclidean.lua) {% 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: