Add solution for problem 44.
This commit is contained in:
parent
76c69e52c1
commit
9bc586b327
|
|
@ -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
|
||||
Loading…
Reference in New Issue