pytudes._2021.leetcode.easy._204__count_primes
https://leetcode.com/problems/count-primes/
- Examples:
>>> Solution().countPrimes(0) 0
Module Contents
Classes
Functions
|
Returns the number of primes between [2, n) |
Returns the number of primes between [2, n) |
- pytudes._2021.leetcode.easy._204__count_primes.count_primes(n)[source]
Returns the number of primes between [2, n)
- Complexity:
Time: O(nlog(logn)) Space: (n) (for the boolean array)
This becomes the bottleneck for large values of n (e.g., ≥ 10^9).
In those cases, a segmented sieve algorithm (either Eratosthenes or Sorenson) or incremental sieve would be more appropriate.
- Args:
n: the limit to which we are to count primes
- Examples:
>>> count_primes(7) 3 >>> count_primes(10) 4 >>> count_primes(0) 0 >>> count_primes(1) 0
- pytudes._2021.leetcode.easy._204__count_primes.count_primes_separate_loop_for_2(n)[source]
Returns the number of primes between [2, n)
- Complexity:
Time: O(nlog(logn)) Space: (n) (for the boolean array)
This becomes the bottleneck for large values of n (e.g., ≥ 10^9).
In those cases, a segmented sieve algorithm (either Eratosthenes or Sorenson) or incremental sieve would be more appropriate.
- Args:
n: the limit to which we are to count primes
- Examples:
>>> count_primes_separate_loop_for_2(7) 3 >>> count_primes_separate_loop_for_2(10) 4 >>> count_primes_separate_loop_for_2(0) 0 >>> count_primes_separate_loop_for_2(1) 0