-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path087-prime-power-triples.hs
More file actions
executable file
·36 lines (26 loc) · 1.03 KB
/
087-prime-power-triples.hs
File metadata and controls
executable file
·36 lines (26 loc) · 1.03 KB
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
29
30
31
32
33
34
35
36
#!/usr/bin/env runhaskell
{- https://projecteuler.net/problem=87
Problem 87
Prime power triples
The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is 28. In fact, there are exactly four numbers below fifty that can be expressed in such a way:
28 = 2^2 + 2^3 + 2^4
33 = 3^2 + 2^3 + 2^4
49 = 5^2 + 2^3 + 2^4
47 = 2^2 + 3^3 + 2^4
How many numbers below fifty million can be expressed as the sum of a prime square, prime cube, and prime fourth power?
-}
import qualified Data.Numbers.Primes as DNP (primes)
import qualified Data.List as DL (sort, group)
upperBound :: Int
upperBound = 50 * 10 ^ 6
nPowers :: Int -> [Int]
nPowers n = takeWhile (< upperBound) . map (^ n) $ DNP.primes
nthPowers :: [[Int]]
nthPowers = map nPowers [4,3,2]
sums :: [Int] -> [Int] -> [Int]
sums (x:xs) yss = map (+ x) yss ++ sums xs yss
sums [] yss = []
uniq :: (Ord a) => [a] -> [a]
uniq = map head . DL.group . DL.sort
main :: IO ()
main = print . length . uniq . filter (< upperBound) . last . scanl1 sums $ nthPowers