Skip to content

Instantly share code, notes, and snippets.

@preprocessor
Created October 17, 2013 18:37
Show Gist options
  • Save preprocessor/7029984 to your computer and use it in GitHub Desktop.
Save preprocessor/7029984 to your computer and use it in GitHub Desktop.
Collatz conjecture in lua
function colConj(n)
local t = {}
if n < 0 then
print("Try a positve number.")
elseif n == 0 then
print("Try a non-zero number.")
else
while n>1 do
if (n%2) == 0 then
n = n/2
else
n = 3*n+1
end
table.insert(t, n)
end
print("Entries: "..#t)
for i=1, #t do
print(t[i])
end
end
end
--[[
Usage: colConj(natural number)
Ex:
> colConj(10)
Entries: 6
5
16
8
4
2
1
>
--]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment