diff --git a/Problem044.hs b/Problem044.hs new file mode 100644 index 0000000..09f083a --- /dev/null +++ b/Problem044.hs @@ -0,0 +1,19 @@ +{-# LANGUAGE ScopedTypeVariables #-} +import Euler + +-- Pentagonal numbers are generated by the formula, P[n]=n(3nāˆ’1)/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