forked from AhmadElsagheer/UVa-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoldbachConjectureII_UVa686.java
More file actions
62 lines (49 loc) · 1.31 KB
/
GoldbachConjectureII_UVa686.java
File metadata and controls
62 lines (49 loc) · 1.31 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
59
60
61
62
package v006;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class GoldbachConjectureII_UVa686{
static ArrayList<Integer> primes;
static boolean[] notPrime;
public static void sieve(int N) {
notPrime = new boolean[N+1];
notPrime[0] = notPrime[1] = true;
primes = new ArrayList<Integer>();
for (int i = 2; i <= N; i++)
if (!notPrime[i])
{
primes.add(i);
for (int j = i * i; j <= N; j += i)
notPrime[j] = true;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
while(true)
{
primes = new ArrayList<Integer>();
String line = br.readLine();
StringTokenizer st = new StringTokenizer(line);
int N = Integer.parseInt(st.nextToken());
if(N==0)
break;
sieve(N);
int count = 0;
int i = 0;
while(true)
{
int prime = primes.get(i++);
if(prime * 2 > N)
break;
int other = N - prime;
if(!notPrime[other])
count++;
}
sb.append(count+"\n");
}
System.out.print(sb);
}
}