Skip to content

Instantly share code, notes, and snippets.

@TannerRogalsky
Created July 11, 2011 04:24
Show Gist options
  • Save TannerRogalsky/1075313 to your computer and use it in GitHub Desktop.
Save TannerRogalsky/1075313 to your computer and use it in GitHub Desktop.
BFG10K program
function middleofsquare(x)
local m = tostring(math.pow(x, 2)):sub(4,9)
m = m:match("^[0]*(%d*)") --trim leading zeros
local offset = 1
while(string.len(m) < 6) do
m = tostring(math.pow(x, 2)):sub(4 - offset,9 - offset)
m = m:match("^[0]*(%d*)") --trim leading zeros
offset = offset + 1
end
return m
end
function findDup(...)
local prev = 0
local array = {...}
table.sort(array)
for _, num in ipairs(array) do
if(num == prev) then
return num
end
prev = num
end
return 0
end
function loop(seed)
local results = {}
table.insert(results, tostring(seed))
while(true) do
table.insert(results, middleofsquare(results[#results]))
dup = findDup(unpack(results))
if (dup ~= 0) then
return seed, dup, #results
end
end
end
--~ Just some test cases to run
print("seed duplicate #results")
for v = 666666, 667000, 1 do
print(loop(v))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment