FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-for-coding-test/20/2.java at master · ndb796/python-for-coding-test · GitHub
ndb796
/
python-for-coding-test
Public
Notifications
You must be signed in to change notification settings
Fork
830
Star
2.4k
Code
Issues
129
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-for-coding-test
/
20
/
2.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
27 lines (25 loc) · 996 Bytes
Breadcrumbs
python-for-coding-test
/
20
/
2.java
Copy path
File metadata and controls
27 lines (25 loc) · 996 Bytes
Raw
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
+
" "
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL