20 lines
725 B
Haskell
20 lines
725 B
Haskell
{-# 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
|