Skip to content

Instantly share code, notes, and snippets.

@jwworth
Created January 25, 2016 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwworth/b7926816974edb2f2354 to your computer and use it in GitHub Desktop.
Save jwworth/b7926816974edb2f2354 to your computer and use it in GitHub Desktop.
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