-
Notifications
You must be signed in to change notification settings - Fork 31
/
PrimeSum.java
48 lines (39 loc) · 953 Bytes
/
PrimeSum.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
package Math;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
/**
* Author - archit.s
* Date - 27/09/18
* Time - 11:45 AM
*/
public class PrimeSum {
public ArrayList<Integer> primesum(int A) {
Set<Integer> s = new HashSet<>();
boolean prime[] = new boolean[A+1];
for(int i=0;i<=A;i++){
prime[i] = true;
}
for(int p=2; p*p<= A; p++){
if(prime[p]){
for(int j=2*p;j<=A;j+=p){
prime[j] = false;
}
}
}
for(int i=2;i<=A;i++){
if(prime[i]){
s.add(i);
}
}
ArrayList<Integer> ans = new ArrayList<>();
for (int i=2;i<=A;i++) {
if(s.contains(i) && s.contains(A-i)){
ans.add(i);
ans.add(A-i);
break;
}
}
return ans;
}
}