forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2.py
More file actions
18 lines (15 loc) ยท 648 Bytes
/
2.py
File metadata and controls
18 lines (15 loc) ยท 648 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import math
n = 1000 # 2๋ถํฐ 1,000๊น์ง์ ๋ชจ๋ ์์ ๋ํ์ฌ ์์ ํ๋ณ
array = [True for i in range(n + 1)] # ์ฒ์์ ๋ชจ๋ ์๊ฐ ์์(True)์ธ ๊ฒ์ผ๋ก ์ด๊ธฐํ
# ์๋ผํ ์คํ
๋ค์ค์ ์ฒด ์๊ณ ๋ฆฌ์ฆ
for i in range(2, int(math.sqrt(n)) + 1): # 2๋ถํฐ n์ ์ ๊ณฑ๊ทผ๊น์ง์ ๋ชจ๋ ์๋ฅผ ํ์ธํ๋ฉฐ
if array[i] == True: # i๊ฐ ์์์ธ ๊ฒฝ์ฐ (๋จ์ ์์ธ ๊ฒฝ์ฐ)
# i๋ฅผ ์ ์ธํ i์ ๋ชจ๋ ๋ฐฐ์๋ฅผ ์ง์ฐ๊ธฐ
j = 2
while i * j <= n:
array[i * j] = False
j += 1
# ๋ชจ๋ ์์ ์ถ๋ ฅ
for i in range(2, n + 1):
if array[i]:
print(i, end=' ')