-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp077.java
More file actions
36 lines (30 loc) · 932 Bytes
/
p077.java
File metadata and controls
36 lines (30 loc) · 932 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
package level04;
import org.junit.Test;
import lib.EulerTest;
public class p077 extends EulerTest {
final int N = 5000;
final int L = 100;
/**
* Find the smallest number that can be written as a sum of primes in over N ways.
* <p>
* <li>dp(i, j) is the number of ways to write i as the sum of primes≤j.
* <li>dp(i, j) = sum_{p≤j} dp(i-p, p)
* <li>This solution is very similar to that of {@link p076}.
*/
@Test
public void test() {
preff(L);
int[][] dp = new int[L + 1][L];
for (int j : range(1, L))
dp[0][j] = 1;
for (int i : rangeC(1, L))
for (int j : range(1, L))
for (int p : rangeC(Math.min(i, j)))
if (isPrime(p))
dp[i][j] += dp[i - p][p];
int ans = 1;
while (dp[ans][ans - 1] <= N)
ans++;
check(ans, 71);
}
}