Skip to content

Instantly share code, notes, and snippets.

@michaelficarra
Last active December 20, 2015 08:19
Show Gist options
  • Select an option

  • Save michaelficarra/6099260 to your computer and use it in GitHub Desktop.

Select an option

Save michaelficarra/6099260 to your computer and use it in GitHub Desktop.
import Data.List (unfoldr)
import Data.Tuple (swap)
import System.IO (hSetBuffering, stdout, BufferMode(..))
placeValues base = reverse . unfoldr (\x -> if x == 0 then Nothing else Just $ swap $ divMod x base)
persistencePath base x = if x < base then [x] else x : (persistencePath base $ product $ placeValues base x)
persistence base n = length (persistencePath base n) - 1
lowestNumberSuchThat f = head $ dropWhile (not . f) [0..]
main = do
hSetBuffering stdout NoBuffering
base <- fmap read $ putStr "base: " >> getLine
targetPersistence <- fmap read $ putStr "persistence: " >> getLine
putStrLn $ show $ lowestNumberSuchThat $ \x -> persistence base x == targetPersistence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment