public class Bj4948 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Integer> inputs = new ArrayList<>();
while (sc.hasNextInt()) {
int input = sc.nextInt();
if (input == 0) break;
inputs.add(input);
}
inputs.sort(Comparator.naturalOrder());
int max = inputs.get(inputs.size()-1) * 2;
boolean[] isPrime = new boolean[max+1];
//true : 소수아님, false : 소수
isPrime[0] = isPrime[1] = true;
for (int i = 2; i*i <= max; i++) {
if (!isPrime[i]) {
for (int j = i*i; j <= max; j += i) {
isPrime[j] = true;
}
}
}
StringBuilder sb = new StringBuilder();
for (int i : inputs) {
int count = 0;
for (int j = i+1; j <= 2*i; j++) {
if (!isPrime[j]) {
count++;
}
}
sb.append(count).append("\n");
}
System.out.println(sb);
}
}
package practice.algorithm.math;
import java.util.Scanner;
public class Bj4948_2 {
private static final int MAX_NUM = 123457;
private static boolean[] isPrime = new boolean[MAX_NUM*2 + 1];
private static int[] primeCount = new int[MAX_NUM*2 + 1];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
getPrime();
getCount();
StringBuilder sb = new StringBuilder();
while (sc.hasNextInt()) {
int n = sc.nextInt();
if (n == 0) break;
sb.append(primeCount[2 * n] - primeCount[n]).append("\n");
}
System.out.println(sb);
}
private static void getCount() {
int count = 0;
for (int i = 2; i < isPrime.length; i++) {
if (!isPrime[i]) {
count++;
}
primeCount[i] = count;
}
}
private static void getPrime() {
//true : 소수 아님, false: 소수임
isPrime[0] = isPrime[1] = true;
for (int i = 2; i <= Math.sqrt(isPrime.length); i++) {
if (!isPrime[i]) {
for (int j = i*i; j < isPrime.length; j += i) {
isPrime[j] = true;
}
}
}
}
}