728x90
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 | import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class Main { private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException{ String[] str = br.readLine().split(" "); boolean[] prime = new boolean[1000001]; prime[0] = prime[1] = true; int A = Integer.parseInt(str[0]); int B = Integer.parseInt(str[1]); for(int i=2; i*i<=B; i++) { if(!prime[i]) { for(int j=i*i; j<=B; j+=i) { prime[j]=true; } } } for(int i=A; i<=B; i++){ if(!prime[i]) System.out.println(i); } // bw.write(""); // bw.flush(); // bw.close(); } } | cs |
'알고리즘 풀이 > 백준' 카테고리의 다른 글
[백준][Java] 1260번 DFS와 BFS (DFS, BFS) (0) | 2021.09.15 |
---|---|
[백준][Java] 2579번 계단 오르기 (DP) (0) | 2021.09.12 |
[백준][Java] 1094번 막대기 (비트마스크) (0) | 2021.09.12 |
[백준][Java] 1080번 행렬 (그리디) (0) | 2021.09.11 |
★ [백준][Java] 2504번 괄호의 값(Stack) #2 (0) | 2021.09.11 |