Problem

Given five positive integer, a < b < c < d < e, there are 10 products for any pair of them. The 3 smallest products are 28, 32, and 56, while the 2 largest products are 128 and 240. Find the value of e.

Solution

One mathematical approach could be finding the ratio among all those numbers, sth along the line: b : c : d = ..., which is not super hard to tackle. The Haskell solution is elegant as well:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import Data.List
import Control.Monad

solution :: [Int]
solution = candidates !! 0
where
candidates = do
e <- [1..240]
d <- [1..e-1]
c <- [1..d-1]
b <- [1..c-1]
a <- [1..b-1]
let sorted = sort $ all_pair [a,b,c,d,e]
guard $ [28, 32, 56] == take 3 sorted
guard $ [128, 240] == (reverse $ take 2 $ reverse sorted)
return [a, b, c, d, e]

all_pair :: [Int] -> [Int]
all_pair [] = []
all_pair (x:xs) = all_pair xs ++ map (*x) xs

main = do
print solution
-- [4,7,8,15,16]