이 기사에서는 아래 주어진 문제 설명에 대한 솔루션에 대해 알아볼 것입니다.
문제 설명 - 숫자 n이 주어지면 n보다 작거나 같은 모든 소수를 인쇄해야 합니다. 제약 조건:n은 작은 숫자입니다.
이제 아래 구현에서 솔루션을 관찰해 보겠습니다 -
예
def SieveOfEratosthenes(n):
# array of type boolean with True values in it
prime = [True for i in range(n + 1)]
p = 2
while (p * p <= n):
# If it remain unchanged it is prime
if (prime[p] == True):
# updating all the multiples
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 1
prime[0]= False
prime[1]= False
# Print
for p in range(n + 1):
if prime[p]:
print (p,end=" ")
# main
if __name__=='__main__':
n = 33
print ("The prime numbers smaller than or equal to", n,"is")
SieveOfEratosthenes(n) 출력
The prime numbers smaller than or equal to 33 is 2 3 5 7 11 13 17 19 23 29 31

모든 변수는 로컬 범위에서 선언되며 해당 참조는 위 그림과 같습니다.
결론
이 기사에서 우리는 에라토스테네스의 체를 위한 Python 프로그램을 만드는 방법에 대해 배웠습니다.