forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp076.java
More file actions
42 lines (33 loc) · 951 Bytes
/
p076.java
File metadata and controls
42 lines (33 loc) · 951 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* Solution to Project Euler problem 76
* Copyright (c) Project Nayuki. All rights reserved.
*
* https://www.nayuki.io/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
import java.math.BigInteger;
public final class p076 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p076().run());
}
public String run() {
return partitions(100, 1).subtract(BigInteger.ONE).toString();
}
private static BigInteger partitions(int n, int k) {
// Dynamic programming
BigInteger[][] table = new BigInteger[n + 1][n + 1];
for (int i = 0; i <= n; i++) {
for (int j = n; j >= 0; j--) {
if (j == i)
table[i][j] = BigInteger.ONE;
else if (j > i)
table[i][j] = BigInteger.ZERO;
else if (j == 0)
table[i][j] = table[i][j + 1];
else
table[i][j] = table[i][j + 1].add(table[i - j][j]);
}
}
return table[n][k];
}
}