Skip to content

Instantly share code, notes, and snippets.

@BrendenHJH
Created May 10, 2018 02:32
Show Gist options
  • Save BrendenHJH/032be68c120cd64729bf283b90c8c5ce to your computer and use it in GitHub Desktop.
Save BrendenHJH/032be68c120cd64729bf283b90c8c5ce to your computer and use it in GitHub Desktop.
백준 4948 베르트랑 공준
import java.util.Scanner;
public class Main {
public static final int MAX = 1000000;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean[] isPrime = new boolean[MAX+1];
for(int i = 2; i <= MAX; i++) {
isPrime[i] = true;
}
for(int i = 2; i <= MAX; i++) {
for(int j = i * 2; j <= MAX; j += i) {
if(!isPrime[j]) continue;
isPrime[j] = false;
}
}
while(true) {
int n = sc.nextInt();
if(n == 0)
break;
int cnt = 0;
for(int i = n+1; i <= 2*n; i++) {
if(isPrime[i])
++cnt;
}
System.out.println(cnt);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment