Largest int from concatenated ints: Difference between revisions

Content added Content deleted
Line 110: Line 110:
Since repeating numerical string "1234" is the same as taking all the digits of 1234/9999 after the decimal point, the following does essentially the same as above:
Since repeating numerical string "1234" is the same as taking all the digits of 1234/9999 after the decimal point, the following does essentially the same as above:
<lang haskell>import Data.List (sortBy)
<lang haskell>import Data.List (sortBy)
import Data.Ord (comparing)
import Data.Ratio ((%))


nines = iterate ((+9).(*10)) 9
nines = iterate ((+9).(*10)) 9


maxcat = foldl (\a (n,d)->a * (1 + d) + n) 0 .
maxcat = foldl (\a (n,d)->a * (1 + d) + n) 0 .
sortBy (\(n1,d1) (n2,d2)->compare (n2*d1) (n1*d2)) .
sortBy (flip $ comparing $ uncurry (%)) .
map (\a->(a, head $ dropWhile (<a) nines))
map (\a->(a, head $ dropWhile (<a) nines))