Skip to content

Instantly share code, notes, and snippets.

@PotcFdk
Last active March 21, 2019 15:15
Show Gist options
  • Save PotcFdk/9ad40999ccbe78380a99c34f06de3f56 to your computer and use it in GitHub Desktop.
Save PotcFdk/9ad40999ccbe78380a99c34f06de3f56 to your computer and use it in GitHub Desktop.
calculating multiplicative persistence in Lua
local function per (n, steps)
steps = steps or 0
if n < 10 then return steps end
local m = 1
for d in string.gmatch (tostring (n), ".") do
m = m * tonumber (d)
end
print ("STEP #" .. steps+1 .. ": " .. n .. " -> " .. m)
return per (m, steps+1)
end
repeat
io.write ("> ")
local n = io.read("*n")
print ("#steps = " .. per (n))
until not n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment