Skip to content

홍성우_Count Primes#44

Open
hsw824 wants to merge 1 commit intomainfrom
hsw1
Open

홍성우_Count Primes#44
hsw824 wants to merge 1 commit intomainfrom
hsw1

Conversation

@hsw824
Copy link
Copy Markdown

@hsw824 hsw824 commented Aug 6, 2025

🧑‍💻 언어 및 제출 결과

  • 사용 언어: TypeScript
  • 통과 여부: ❌

🧠 풀이 설명

  • 에라토스테네스의 체를 알고리즘으로 구현
  • 해당 내용을 텍스트만 보고 구현해봄
function countPrimes(n: number): number {
  const result = [];
  let i = 2;

  while (i <= n) {}

  for (let i = 2; i <= n; i++) {
    if (i !== 2 && i % 2 === 0) {
      continue;
    }
    if (i !== 3 && i % 3 === 0) {
      continue;
    }
    if (i !== 5 && i % 5 === 0) {
      continue;
    }
    if (i !== 7 && i % 7 === 0) {
      continue;
    }
    if (i !== 11 && i % 11 === 0) {
      continue;
    }
    result.push(i);
  }

  return result.length;
}
  • 이렇게 할 경우 제외할 숫자를 계속 조건에 추가해야하기 때문에 좋지 않고 Time Limit Exceeded가 뜰 것 같다는 생각을 함
  • gpt한테 물어봐서 진행

정답 코드

function countPrimes(n: number): number {
  if (n <= 2) return 0;

  const isPrime = new Array(n).fill(true);
  isPrime[0] = false;
  isPrime[1] = false;

  for (let i = 2; i * i < n; i++) {
    if (isPrime[i]) {
      for (let j = i * i; j < n; j += i) {
        isPrime[j] = false;
      }
    }
  }

  return isPrime.filter(Boolean).length;
}

📊 시간/공간 복잡도

✅ 어떠한 근거로 시간/공간 복잡도가 이렇게 나왔는지 설명해주세요.

⚡️ 풀이의 속도와 메모리 등을 캡쳐해서 올려주세요.

  • 시간 복잡도: O(n)
  • 공간 복잡도: O(1)

📝 추가 설명 (선택)

  • 고민했던 포인트가 있다면 간단히 적어주세요.

🙋‍♂️ 리뷰어에게

  • 리뷰어가 보면 좋을 포인트, 질문, 궁금한 점 등을 작성해 주세요.

@github-actions github-actions bot added the TeamB Team label for TeamB label Aug 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

TeamB Team label for TeamB

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant