forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2.java
More file actions
27 lines (25 loc) ยท 996 Bytes
/
2.java
File metadata and controls
27 lines (25 loc) ยท 996 Bytes
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
import java.util.*;
class Main {
public static int n = 1000; // 2๋ถํฐ 1,000๊น์ง์ ๋ชจ๋ ์์ ๋ํ์ฌ ์์ ํ๋ณ
public static boolean[] arr = new boolean[n + 1];
public static void main(String[] args) {
Arrays.fill(arr, true); // ์ฒ์์ ๋ชจ๋ ์๊ฐ ์์(True)์ธ ๊ฒ์ผ๋ก ์ด๊ธฐํ(0๊ณผ 1์ ์ ์ธ)
// ์๋ผํ ์คํ
๋ค์ค์ ์ฒด ์๊ณ ๋ฆฌ์ฆ ์ํ
// 2๋ถํฐ n์ ์ ๊ณฑ๊ทผ๊น์ง์ ๋ชจ๋ ์๋ฅผ ํ์ธํ๋ฉฐ
for (int i = 2; i <= Math.sqrt(n); i++) {
// i๊ฐ ์์์ธ ๊ฒฝ์ฐ(๋จ์ ์์ธ ๊ฒฝ์ฐ)
if (arr[i] == true) {
// i๋ฅผ ์ ์ธํ i์ ๋ชจ๋ ๋ฐฐ์๋ฅผ ์ง์ฐ๊ธฐ
int j = 2;
while (i * j <= n) {
arr[i * j] = false;
j += 1;
}
}
}
// ๋ชจ๋ ์์ ์ถ๋ ฅ
for (int i = 2; i <= n; i++) {
if (arr[i]) System.out.print(i + " ");
}
}
}