Created
January 25, 2016 23:47
-
-
Save jwworth/b7926816974edb2f2354 to your computer and use it in GitHub Desktop.
From One End To The Other (Lua)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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