Skip to content

Instantly share code, notes, and snippets.

@vaibhavsagar
Last active September 8, 2016 13:35
Show Gist options
  • Save vaibhavsagar/de95423032e44cfeab52b6dd9b74409a to your computer and use it in GitHub Desktop.
Save vaibhavsagar/de95423032e44cfeab52b6dd9b74409a to your computer and use it in GitHub Desktop.
Solution for code dojo 30/08/2016 with @miguel-vila
-- Slow solution that fails for n>19 due to integer overflow
import Data.List
import Data.Ord
maxPair n = sortBy (comparing snd) [(x,y) | x <- [0,3..n], y <- [0,5..(n-x)], x+y==n]
gen [] = -1
gen ((x,y):_) = let
noOf5s = (replicate x '5')
noOf3s = (replicate y '3')
in read (noOf5s ++ noOf3s) :: Int
largest n = gen $ maxPair n
-- Faster solution that passes all test cases:
import Data.Maybe
maxPair' n = listToMaybe [(x,y) | x <- [0,5..n], y <- [0,3..(n-x)], x+y==n]
gen' Nothing = "-1"
gen' (Just (x,y)) = let
noOf5s = (replicate y '5')
noOf3s = (replicate x '3')
in (noOf5s ++ noOf3s)
largest' n = gen' $ maxPair' n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment