Skip to content

Commit 9f0826e

Browse files

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

count-primes.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
204. Count Primes
3+
- https://leetcode.com/problems/count-primes/
4+
- https://leetcode.com/explore/challenge/card/may-leetcoding-challenge-2021/599/week-2-may-8th-may-14th/3738/
5+
"""
6+
7+
# Method 1
8+
# Using Sieve of Eratosthenes algorithm
9+
10+
class Solution:
11+
def countPrimes(self, n: int) -> int:
12+
if n < 2:
13+
return 0
14+
isPrime = [1] * n
15+
isPrime[0] = isPrime[1] = 0
16+
for i in range(2, n):
17+
if isPrime[i]:
18+
for j in range(i * i, n, i):
19+
isPrime[j] = 0
20+
return sum(isPrime)
21+

0 commit comments

Comments
 (0)