-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeSummations.java
More file actions
52 lines (43 loc) · 1.48 KB
/
PrimeSummations.java
File metadata and controls
52 lines (43 loc) · 1.48 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
49
50
51
52
package dev;
public class PrimeSummations {
public static void main(String[] args) {
int targetWays = 5000;
int[] primes = generatePrimesUpTo(targetWays); // Generate primes up to the targetWays
int[] ways = new int[1000000]; // Assuming the answer is within 1 million
ways[0] = 1; // Base case: there is one way to make 0 (using no primes)
for (int prime : primes) {
for (int i = prime; i < ways.length; i++) {
ways[i] += ways[i - prime];
}
}
int result = 0;
while (ways[result] <= targetWays) {
result++;
}
System.out.println("The first value that can be written as the sum of primes in over " + targetWays + " different ways is: " + result);
}
private static int[] generatePrimesUpTo(int n) {
boolean[] isComposite = new boolean[n + 1];
for (int i = 2; i * i <= n; i++) {
if (!isComposite[i]) {
for (int j = i * i; j <= n; j += i) {
isComposite[j] = true;
}
}
}
int count = 0;
for (boolean composite : isComposite) {
if (!composite) {
count++;
}
}
int[] primes = new int[count - 2]; // Exclude 0 and 1
int index = 0;
for (int i = 2; i <= n; i++) {
if (!isComposite[i]) {
primes[index++] = i;
}
}
return primes;
}
}