Last active
March 21, 2019 15:15
-
-
Save PotcFdk/9ad40999ccbe78380a99c34f06de3f56 to your computer and use it in GitHub Desktop.
calculating multiplicative persistence in 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
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