Skip to content

Adding Euclidean Algorithm in Lua #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@
{
"lang": "ti83b",
"name": "TI-83 Basic"
},
{
"lang": "lua",
"name": "Lua"
}
]
}
Expand Down
32 changes: 32 additions & 0 deletions contents/euclidean_algorithm/code/lua/euclidean.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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
a, b = b, a%b
end

return a
end

function main ()
print(euclidSub(128 * 12, 128 * 77))
print(euclidMod(64 * 67, 64 * 81))
end

main()
6 changes: 6 additions & 0 deletions contents/euclidean_algorithm/euclidean_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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-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:
Expand Down Expand Up @@ -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 %}


Expand Down