import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
boolean[] isPrime = new boolean[n+1];
Arrays.fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i*i <= n; i++) {
if (isPrime[i]) {
for (int j = i*i; j <= n; j += i) {
isPrime[j] = false;
}
}
}
for (int i = 0; i <= n; i++) {
if (isPrime[i] && i >= m) {
System.out.println(i);
}
}
}
}
string builder 로 답안 모두 더하여 한번만 출력하는 방법
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static boolean[] prime;
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
int m = Integer.parseInt(st.nextToken());
int n = Integer.parseInt(st.nextToken());
prime = new boolean[n+1];
prime();
StringBuilder sb = new StringBuilder();
for (int i = m; i <=n; i++) {
if(!prime[i]) {
sb.append(i).append('\n');
}
}
System.out.print(sb);
}
private static void prime() {
//false = 소수, true = 소수가 아님
prime[0] = prime[1] = true;
for (int i = 2; i <= Math.sqrt(prime.length); i++) {
if (prime[i]) {
continue;
}
for (int j = i*i; j < prime.length; j += i) {
prime[j] = true;
}
}
}
}