Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
27 lines (25 loc) · 996 Bytes

File metadata and controls

27 lines (25 loc) · 996 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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 + " ");
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.