↓Skip to main content
  1. LeetCode/

LeetCode 204: Count Primes

·2 mins· ·
LeetCode Medium Array Math Enumeration Number-Theory
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-11-11

  • Submissions: Output Limit Exceeded → Time Limit Exceeded ×4 → Runtime Error ×2 → Accepted → Wrong Answer → Accepted ×2

Approach
#

The sieve of Eratosthenes (Number Theory Template 3): start with all True, and from 2 on, only a p still marked prime crosses out its multiples, beginning at p² (smaller multiples were crossed by smaller primes); p only needs to reach √n. In Python slice assignment is_prime[p*p::p] = ... is far faster than an inner loop.

My Attempts
#

This took 11 submissions:

  • The first 2022-11-11 version left a print in (Output Limit Exceeded). The next ones sieved, but with if num: every number crossed out its multiples, prime or not, and the outer loop ran to n: four TLEs.
  • On 2022-11-17 I looped only to √n with slice assignment; a misspelled name (NameError) and prime[1] out of range for n < 2 (IndexError) came first, then the special case passed.
  • A 2026 rewrite as a linear sieve first tested if is_composite[i], the opposite condition, so no prime was ever added: n = 3 returned 0 instead of 1.

Solution
#

class Solution:
    def countPrimes(self, n: int) -> int:
        if n < 3:
            return 0
        is_prime = [True] * n
        is_prime[0] = is_prime[1] = False
        for p in range(2, int(n ** 0.5) + 1):
            if is_prime[p]:
                is_prime[p * p::p] = [False] * len(range(p * p, n, p))
        return sum(is_prime)

Complexity
#

Time O(n log log n), space O(n).

Mistakes to Avoid
#

  • Sieve only with primes: a composite’s multiples were already crossed out by its prime factors.
  • The outer loop only needs √n.
  • For n < 2 the array is too short and is_prime[1] is out of range.
  • In a linear sieve a number joins the prime list when it is not composite; inverting that finds no primes.

Interview Explanation
#

I use the sieve of Eratosthenes: for p from 2 to √n, if p is still prime I cross out its multiples from p², then count what remains. Time O(n log log n).

Related

LeetCode 189: Rotate Array
·2 mins
LeetCode Medium Array Math Two-Pointers
LeetCode 189 note: take k %= n, then reverse three times: the whole array, the first k, and the last n - k, with O(1) extra space
LeetCode 48: Rotate Image
·2 mins
LeetCode Medium Array Math Matrix
LeetCode 48 note: rotating 90 degrees clockwise is a transpose followed by reversing each row, both in place
LeetCode 973: K Closest Points to Origin
·1 min
LeetCode Medium Array Math Divide-and-Conquer Geometry
LeetCode 973 note: as in Top-K, keep a size-k max-heap (negated distances) holding the k closest points
LeetCode 134: Gas Station
·2 mins
LeetCode Medium Array Greedy
LeetCode 134 note: if total gas is short there is no answer; otherwise scan, and whenever the tank goes negative move the start to the next station
LeetCode 162: Find Peak Element
·2 mins
LeetCode Medium Array Binary-Search
LeetCode 162 note: nums[mid] < nums[mid + 1] guarantees a peak to the right (rising until the edge at worst), otherwise one at or left of mid
LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal
·1 min
LeetCode Medium Array Hash-Table Divide-and-Conquer Tree
LeetCode 106 note: the last post-order value is the root; taking roots from the end of the post-order, build the right subtree before the left