Sieve of Eratosthenes

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



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Does Background Music Matter to Speech in Language Models
  • Measuring the Checker
  • Mastermind
  • Resillience
  • Multi-Head Attention