-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path044-pentagon-numbers.hs
More file actions
executable file
·28 lines (19 loc) · 956 Bytes
/
044-pentagon-numbers.hs
File metadata and controls
executable file
·28 lines (19 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env runhaskell
{- https://projecteuler.net/problem=44
Problem 44
Pentagon numbers
Pentagonal numbers are generated by the formula, P_n=n(3n−1)/2. The first ten pentagonal numbers are:
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
It can be seen that P_4 + P_7 = 22 + 70 = 92 = P_8. However, their difference, 70 − 22 = 48, is not pentagonal.
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?
-}
import qualified Data.Set as DS
-- This was a guess. Turned out to be spot on.
upperBound :: Int
upperBound = 10000
pentagonals :: [Int]
pentagonals = map (\n -> n * (3 * n - 1) `quot` 2) [1..upperBound]
pentagonals' :: DS.Set Int
pentagonals' = DS.fromList pentagonals
main :: IO ()
main = print . head $ [q - p | p <- pentagonals, q <- filter (> p) pentagonals, DS.member (q - p) pentagonals', DS.member (q + p) pentagonals']