27 lines
883 B
Haskell
27 lines
883 B
Haskell
-- Take the number 192 and multiply it by each of 1, 2, and 3:
|
||
--
|
||
-- 192 × 1 = 192
|
||
-- 192 × 2 = 384
|
||
-- 192 × 3 = 576
|
||
--
|
||
-- By concatenating each product we get the 1 to 9 pandigital, 192384576. We
|
||
-- will call 192384576 the concatenated product of 192 and (1,2,3).
|
||
--
|
||
-- What is the largest 1 to 9 pandigital 9-digit number that can be formed as
|
||
-- the concatenated product of an integer with (1,2, ... , n) where n > 1?
|
||
|
||
import Euler
|
||
|
||
pandigital n = sort (toDigits n) == [1..9]
|
||
|
||
concatProduct n = fromDigits $ concat $ zipWith const products upTo9
|
||
where
|
||
products = map (toDigits . (n*)) [1..]
|
||
lengths = map length products
|
||
totals = scanl1 (+) lengths
|
||
upTo9 = takeWhile (<= 9) totals
|
||
|
||
main = print $ maximum $ [ n | n <- map concatProduct [1..9999]
|
||
, n >= 100000000
|
||
, pandigital n ]
|