forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp078.java
More file actions
48 lines (39 loc) · 1.15 KB
/
p078.java
File metadata and controls
48 lines (39 loc) · 1.15 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
37
38
39
40
41
42
43
44
45
46
47
48
/*
* Solution to Project Euler problem 78
* Copyright (c) Project Nayuki. All rights reserved.
*
* https://www.nayuki.io/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p078 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p078().run());
}
private static final int MODULUS = Library.pow(10, 6);
public String run() {
for (int limit = 1; ; limit *= 2) {
int result = search(limit);
if (result != -1)
return Integer.toString(result);
}
}
private static int search(int limit) {
/*
* partitions[i] is {the number of ways i can be written
* as an unordered sum of positive integers} mod 10^6.
* Note that the partition function P(n, k) can be computed with
* dynamic programming using only 1 dimension for memoization.
*/
int[] partitions = new int[limit];
partitions[0] = 1;
for (int i = 1; i < limit; i++) {
for (int j = i; j < limit; j++)
partitions[j] = (partitions[j] + partitions[j - i]) % MODULUS;
}
for (int i = 0; i < limit; i++) {
if (partitions[i] == 0)
return i;
}
return -1;
}
}