20 lines
837 B
Haskell
20 lines
837 B
Haskell
-- Find the sum of the only ordered set of six cyclic 4-digit numbers for which
|
|
-- each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and
|
|
-- octagonal, is represented by a different number in the set.
|
|
|
|
import Euler
|
|
|
|
polygonal p n = n * ((p - 2) * n - (p - 4)) `div` 2
|
|
findPolygonal (l,h) ps = [ (x, ps \\ [p]) | p <- ps, x <- takeWhile (<= h) $ dropWhile (< l) $ map (polygonal p) [1..] ]
|
|
|
|
main = print $ head $ do
|
|
let findNext x ps = findPolygonal (max 1000 $ 100 * (x `mod` 100), 100 * (x `mod` 100) + 99) ps
|
|
(a, ps') <- findPolygonal (1000, 9999) [8,7..3]
|
|
(b, ps') <- findNext a ps'
|
|
(c, ps') <- findNext b ps'
|
|
(d, ps') <- findNext c ps'
|
|
(e, ps') <- findNext d ps'
|
|
(f, ps') <- findNext e ps'
|
|
guard $ a >= 100 * (f `mod` 100) && a <= 100 * (f `mod` 100) + 99
|
|
return $ (id &&& sum) [a,b,c,d,e,f]
|