Skip to content

Commit 1880be2

Browse files
authored
Create goldbach's conjecture
1 parent 354ecfd commit 1880be2

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

prime sum/goldbach's conjecture

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.util.*;
2+
public class Solution {
3+
public ArrayList<Integer> primesum(int a) {
4+
ArrayList<Integer> sum = new ArrayList<Integer>();
5+
int primes[] = new int[a+1];
6+
int isSum,flag=1;
7+
ArrayList<Integer> primeList = new ArrayList<Integer>();
8+
int i,j,n;
9+
10+
for(i=0;i<=a;i++)
11+
{
12+
primes[i]=1;
13+
}
14+
primes[0]=0;
15+
primes[1]=0;
16+
17+
for(i=2;i<=(int)Math.sqrt(a);i++)
18+
{
19+
if(primes[i]==1)
20+
{
21+
for(j=2;j*i<=a;j++)
22+
23+
{
24+
primes[i*j]=0;
25+
}
26+
}
27+
}
28+
29+
for(i=0;i<=a;i++)
30+
{
31+
if(primes[i]==1)
32+
{
33+
primeList.add(i);
34+
35+
}
36+
}
37+
for (i=0;i<primeList.size();i++)
38+
{
39+
if(flag==1)
40+
{
41+
for(j=0;j<primeList.size();j++)
42+
{
43+
isSum=primeList.get(i)+ primeList.get(j);
44+
if (isSum== a)
45+
{
46+
sum.add(primeList.get(i));
47+
sum.add(primeList.get(j));
48+
flag=0;
49+
}
50+
}
51+
}
52+
53+
}
54+
55+
return sum;
56+
57+
}
58+
}

0 commit comments

Comments
 (0)