-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPrimeSummations.java
81 lines (67 loc) · 1.76 KB
/
PrimeSummations.java
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.io.*;
import java.util.*;
public class Solution {
private static List<Integer> primeList = new ArrayList<Integer>();
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int tests = s.nextInt();
populatePrimeList();
for (int i = 0; i < tests; i++) {
process(s.nextInt());
}
}
private static void populatePrimeList() {
for (int i = 0; i < 1000; i++) {
if (isPrime(i)) {
primeList.add(i);
}
}
}
private static boolean isPrime(int n) {
if (n <= 3) {
return n > 1;
} else if (n % 2 == 0 || n % 3 == 0) {
return false;
} else {
double sqrtN = Math.floor(Math.sqrt(n));
for (int i = 5; i <= sqrtN; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
}
private static void process(int num) {
System.out.println(count(primeList, findIndex(num), num));
}
private static int findIndex(int num) {
int i = 0;
for (;i < primeList.size(); i++) {
if (primeList.get(i) > num) {
break;
}
}
return i;
}
private static long count(List<Integer> array, int m, int n) {
long[] currentArray = new long[n+1];
long[] previousArray = null;
currentArray[0] = 1;
for (int i = 0; i < m; i++) {
for (int j = 1; j < n+1; j++) {
long x = 0;
if (j-array.get(i) >= 0) {
x = currentArray[j-array.get(i)];
}
long y = 0;
if (previousArray != null) {
y = previousArray[j];
}
currentArray[j] = x + y;
}
previousArray = currentArray;
}
return currentArray[n];
}
}