Skip to content

Instantly share code, notes, and snippets.

@cacharle
Created August 18, 2019 11:51
Show Gist options
  • Save cacharle/fc17d709a9cd54d222e6aee38fb3da5f to your computer and use it in GitHub Desktop.
Save cacharle/fc17d709a9cd54d222e6aee38fb3da5f to your computer and use it in GitHub Desktop.
-- check the multiplicative persistence of a given number
-- https://www.youtube.com/watch?v=Wim9WJeDTHQ&list=PLt5AfwLFPxWKuRpivZd_ivR2EvEzKrDUu&index=2
-- https://oeis.org/A003001
import System.Environment(getArgs)
import Data.List(intercalate)
main = do
args <- getArgs
let n = read (head args) :: Integer
nMult = selfMult n
putStrLn (intercalate "\n" (map show nMult))
putStrLn (show n ++ " has a multiplicative persistence of " ++ (show $ length nMult - 1))
selfMult :: (Integral a) => a -> [a]
selfMult n
| length ndigits < 2 = [n]
| otherwise = n : (selfMult $ product ndigits)
where ndigits = digits n
digits 0 = []
digits x = digits (x `div` 10) ++ [x `mod` 10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment