-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeSum.java
More file actions
58 lines (50 loc) · 1.07 KB
/
PrimeSum.java
File metadata and controls
58 lines (50 loc) · 1.07 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
53
54
55
56
57
58
import java.util.*;
public class Solution {
public ArrayList<Integer> primesum(int a) {
ArrayList<Integer> sum = new ArrayList<Integer>();
int primes[] = new int[a+1];
int isSum,flag=1;
ArrayList<Integer> primeList = new ArrayList<Integer>();
int i,j,n;
for(i=0;i<=a;i++)
{
primes[i]=1;
}
primes[0]=0;
primes[1]=0;
for(i=2;i<=(int)Math.sqrt(a);i++)
{
if(primes[i]==1)
{
for(j=2;j*i<=a;j++)
{
primes[i*j]=0;
}
}
}
for(i=0;i<=a;i++)
{
if(primes[i]==1)
{
primeList.add(i);
}
}
for (i=0;i<primeList.size();i++)
{
if(flag==1)
{
for(j=0;j<primeList.size();j++)
{
isSum=primeList.get(i)+ primeList.get(j);
if (isSum== a)
{
sum.add(primeList.get(i));
sum.add(primeList.get(j));
flag=0;
}
}
}
}
return sum;
}
}