Add solution for problem 44.

This commit is contained in:
jdmcdona 2015-08-14 17:05:06 -05:00
parent 76c69e52c1
commit 9bc586b327
1 changed files with 19 additions and 0 deletions

19
Problem044.hs Normal file
View File

@ -0,0 +1,19 @@
{-# LANGUAGE ScopedTypeVariables #-}
import Euler
-- Pentagonal numbers are generated by the formula, P[n]=n(3n1)/2.
--
-- Find the pair of pentagonal numbers, P[j] and P[k], for which their sum and
-- difference are pentagonal and D = |P[k] P[j]| is minimised; what is the value
-- of D?
pentagonals = map (\n -> n * (3*n - 1) `div` 2) [1..]
isPentagonal x = let n = (sqrt (24 * fromIntegral x + 1) + 1) / 6 in n == fromIntegral (floor n) && n > 0
main = print $ minimum $ do
-- Assumption: The solution will not require considering pentagonals over 10^7.
b <- takeWhile (< 10000000) pentagonals
a <- takeWhile (< b) pentagonals
guard $ isPentagonal (a + b)
guard $ isPentagonal (b - a)
return $ b - a