WEEK 10 COUNT PRIMES
Leetcode Problem: 204
Problem Statement
 Given an integer n, return the number of prime numbers that are
strictly less than n.
Examples
 Example 1:
 Input: n = 10
 Output: 4
 Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
 Example 2:
 Input: n = 0
 Output: 0
 Example 3:
 Input: n = 1
 Output: 0
Solution
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
class Solution:
def countPrimes(self, n: int) -> int:
if n < 2:
return 0
is_prime = [True] * n
is_prime[0] = is_prime[1] = False
for i in range(2, int(n ** 0.5) + 1):
if is_prime[i]:
for j in range(i * i, n, i):
is_prime[j] = False
return sum(is_prime)
Python Solution
Initially set all numbers as Prime
Python's range() is exclusive at the
end, it stops before the upper limit.
class Solution:
def countPrimes(self, n: int) -> int:
if n < 2:
return 0
is_prime = [True] * n
is_prime[0] = is_prime[1] = False
for i in range(2, int(n ** 0.5) + 1):
if is_prime[i]:
for j in range(i * i, n, i):
is_prime[j] = False
return sum(is_prime)
Walkthrough
0 1 2 3 4 5 6 7 8 9 10
n = 10
T T T T T T T T T T T
F F T T T T T T T T T
i = 2
√10 = 3
True
F F T T F T F T F T F
i = 3
F F T T F T F T F F F
Solution: 4
Java Solution
class Solution {
public int countPrimes(int n) {
if (n < 2)
return 0;
boolean[] isPrime = new boolean[n];
for (int i = 2; i < n; i++)
isPrime[i] = true;
for (int i = 2; i * i < n; i++) {
if (isPrime[i]) {
for (int j = i * i; j < n; j += i)
isPrime[j] = false;
}
}
int count = 0;
for (int i = 2; i < n; i++) {
if (isPrime[i])
count++;
}
return count;
}
}
MEDIUM COUNT PRIMES Leetcode Step by Step
MEDIUM COUNT PRIMES Leetcode Step by Step

MEDIUM COUNT PRIMES Leetcode Step by Step

  • 1.
    WEEK 10 COUNTPRIMES Leetcode Problem: 204
  • 2.
    Problem Statement  Givenan integer n, return the number of prime numbers that are strictly less than n.
  • 3.
    Examples  Example 1: Input: n = 10  Output: 4  Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.  Example 2:  Input: n = 0  Output: 0  Example 3:  Input: n = 1  Output: 0
  • 4.
    Solution 1 2 34 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  • 5.
    class Solution: def countPrimes(self,n: int) -> int: if n < 2: return 0 is_prime = [True] * n is_prime[0] = is_prime[1] = False for i in range(2, int(n ** 0.5) + 1): if is_prime[i]: for j in range(i * i, n, i): is_prime[j] = False return sum(is_prime) Python Solution Initially set all numbers as Prime Python's range() is exclusive at the end, it stops before the upper limit.
  • 6.
    class Solution: def countPrimes(self,n: int) -> int: if n < 2: return 0 is_prime = [True] * n is_prime[0] = is_prime[1] = False for i in range(2, int(n ** 0.5) + 1): if is_prime[i]: for j in range(i * i, n, i): is_prime[j] = False return sum(is_prime) Walkthrough 0 1 2 3 4 5 6 7 8 9 10 n = 10 T T T T T T T T T T T F F T T T T T T T T T i = 2 √10 = 3 True F F T T F T F T F T F i = 3 F F T T F T F T F F F Solution: 4
  • 7.
    Java Solution class Solution{ public int countPrimes(int n) { if (n < 2) return 0; boolean[] isPrime = new boolean[n]; for (int i = 2; i < n; i++) isPrime[i] = true; for (int i = 2; i * i < n; i++) { if (isPrime[i]) { for (int j = i * i; j < n; j += i) isPrime[j] = false; } } int count = 0; for (int i = 2; i < n; i++) { if (isPrime[i]) count++; } return count; } }