강의

멘토링

커뮤니티

Inflearn Community Q&A

yurilee49427448's profile image
yurilee49427448

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

7. Number of prime numbers (Sieve of Eratosthenes)

소수의개수)for문에서 잘 모르겠는 부분이 있어 질문 드립니다.

Written on

·

276

0

for j in range(i,n+1,i):

    ch[j]=1

에서 2와 3은 소수인데 소수가 아닌걸로 돼서

ch[i]==0일때 카운트가 되지 못하지 않나요?

이 부분을 제가 잘 이해하지 못한거같아서 질문드립니다. 답변부탁드립니다.

코테 준비 같이 해요! python

Answer 1

1

codingcamp님의 프로필 이미지
codingcamp
Instructor

i가 2일 때 if ch[i]==0: 이 참이되어 카운팅하고 j for문이 2부터 2의 배수들을 다 1로 체크합니다. 그 다음 i가 3일때도 ch[i]는 0값으로 있겠죠. n을 10정도로 해놓고 연필들고 스스로 i가 2일때 3일때 시뮬레이션 해보세요.

yurilee49427448's profile image
yurilee49427448

asked

Ask a question