μ΄μ μ λͺ» νΌ λ¬Έμ λ€μ νμλ€..
import sys
M, N = map(int, sys.stdin.readline().split())
table = list(range(2, N + 1))
i = 0
while i*i <= N:
j = 2
while table[i] * j <= N:
rem = table[i] * j
if rem in table:
table.remove(rem)
j += 1
i += 1
for x in table:
if x >= M:
print(x)
μκ° μ νμ΄ 2μ΄
λΌμ μΌλ°μ μΌλ‘ μμ ꡬνλ λ°©λ²μΌλ‘ μ λ κ±°λΌ μκ°ν΄μ κ²μν΄λ³΄λ, μλΌν μ€ν
λ€μ€μ 체
λΌλ κ°λ
μ΄ μμλ€.
μ΄ κ°λ
μΌλ‘ μ½λλ₯Ό μμ±ν 건λ°, μ΄κ² μκ°μ΄κ³Ό
λΌμ λΉν©νλ€.
import sys
def delete(first, step):
if first + step > len(table) - 1:
return 0
table[first + step] = 0
delete(first + step, step)
# input
M, N = map(int, sys.stdin.readline().split())
table, first = [1] * (N + 1), 2
# process
while first * first <= N:
delete(first, first)
first += 1
# output
for i in range(M, N + 1):
if table[i]:
print(i)
μ΄λ²μλ μλΌν μ€ν
λ€μ€μ 체
μ μ¬κ·λ‘ ꡬννλ€.
νμ§λ§ μ΄ μ½λλ λ°νμ μλ¬ (RecursionError)
κ° λ΄λ€.
python
μ μ¬κ· depth
κ° 3000
μΈλ° μ΄κ±Έ λ겨μ μμ μ€λ₯κ° λ¬ λͺ¨μμ΄λ€.
n, m = map(int, input().split())
for x in range(n, m+1):
flag = 0
for i in range(2, x//2+1):
if x % i == 0:
flag += 1
break
if not flag:
print(x)
λ€λ₯Έ λΆ λΈλ‘κ·Έ μ½λλ₯Ό νμ΄λ³΄λ, μΌλ°μ μΈ λ°©λ²μΌλ‘ μμ±ν μ½λκ°μκ°μ΄κ³Ό
λ¨μ§ μμλ€.
κ·Έλμ μΌλ°μ μΈ λ°©λ²μΌλ‘ μμλ₯Ό ꡬν΄λ΄€λλ°, μ΄λ²μλ μκ°μ΄κ³Ό
λ΄λ€.
γ γ ...
import sys
sys.setrecursionlimit(10000000)
def delete(first, step):
if first + step > len(table) - 1:
return 0
table[first + step] = 0
delete(first + step, step)
# input
M, N = map(int, sys.stdin.readline().split())
table, first = [1] * (N + 1), 2
table[1] = 0
# process
while first * first <= N:
delete(first, first)
first += 1
# output
for i in range(M, N + 1):
if table[i]:
print(i)
νλ μ νλ €μ π Try 2
μ μ¬κ· μ½λμ depth limit
μ 10000000
μΌλ‘ λλ Έλ€. κ·Έλ¬λλ, λ§μμ΅λλ€!!
κ° λ¨κΈ΄ νλ€.
νμ§λ§ κ°μ΄μΉ μμ λλ..
def isPrime(num):
if num==1:
return False
else:
for i in range(2, int(num**0.5)+1):
if num%i == 0:
return False
return True
M, N = map(int, input().split())
for i in range(M, N+1):
if isPrime(i):
print(i)
λ€λ₯Έ λΆ μ½λλ₯Ό κ°μ Έμλ€.
μ΄ μ½λλ μμλ₯Ό νλ³νλ κ°μ₯ μΌλ°μ μΈ λ°©λ²μΌλ‘ μμ±λλ€.
μλμ κ°μ λ΄μ©μ λ°°μΈ μ μμλ€.
if num == 1: return False
1
λΆν° νλ³ν΄μΌ νλ μμΈμ μΈ μν©μΌ λλ₯Ό λλΉν μ½λ
int(num**0.5)
μ΄λ€ μμ
루νΈ
λ₯Όmath
λΌμ΄λΈλ¬λ¦¬ μμ΄λ μ΄λ° λ°©λ²μΌλ‘ ꡬν μ μλ€.
[python νμ΄μ¬] λ°±μ€ 1929λ²: μμ ꡬνκΈ°
https://deokkk9.tistory.com/17
μμ ꡬνκΈ° (μλΌν μ€ν
λ€μ€μ 체)
https://jongmin92.github.io/2017/11/05/Algorithm/Concept/prime/