forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp077.hs
31 lines (27 loc) · 999 Bytes
/
p077.hs
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
{-
- Solution to Project Euler problem 77
- Copyright (c) Project Nayuki. All rights reserved.
-
- https://www.nayuki.io/page/project-euler-solutions
- https://github.com/nayuki/Project-Euler-solutions
-}
import qualified EulerLib
target = 5000
main = putStrLn (show ans)
ans = head $ dropWhile (\n -> partitions n n <= target) [0..]
{-
- Let P(i, n) denote the number of ways that n can be written as an
- unordered sum of prime numbers where no prime is greater than i.
-
- * P(i, 0) = 1 for all i.
- * P(0, n) = 0 for all n > 0.
- * If i is 1 or composite then P(i, n) = P(i - 1, n).
- * Otherwise i is prime:
- * If i <= n then P(i, n) = P(i - 1, n) + P(i, n - i).
- * Else P(i, n) = P(i - 1, n).
-}
partitions :: Int -> Int -> Int
partitions _ 0 = 1
partitions 0 _ = 0
partitions i n = (if ((EulerLib.isPrime i) && i <= n) then (partitionsMemo !! i !! (n - i)) else 0) + (partitionsMemo !! (i - 1) !! n)
partitionsMemo = [[partitions i n | n <- [0..]] | i <- [0..]]