Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
From One End To The Other (Lua)
-- Week 4: From One End to the Other: Find the smallest possible (positive)
-- integer that ends in a six such that if that six is removed and placed in front
-- of the remaining digits of the number, the resulting number will be four times
-- as large as the original.
function swap_first_and_last(t, n)
table.insert(t, 1, n)
return tonumber(table.concat(t))
end
for i = 6, 1000000, 2 do
local digits = {}
local count = 1
for x in string.gmatch(tostring(i), "%S") do
table.insert(digits, count, x)
count = count + 1
end
local last = table.remove(digits)
if last == "6" and swap_first_and_last(digits, last) == i * 4 then
print(i)
os.exit()
end
end
-- $ lua from_one_end_to_the_other.lua
-- 153846
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.