Add solution for problem 61.

This commit is contained in:
jdmcdona 2015-09-04 14:13:45 -05:00
parent 6996016ccc
commit 9c24c2f0d2
1 changed files with 19 additions and 0 deletions

19
Problem061.hs Normal file
View File

@ -0,0 +1,19 @@
-- 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]