-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount Primes.py
35 lines (26 loc) · 1.08 KB
/
Count Primes.py
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
# https://leetcode.com/problems/count-primes/
class Solution:
def countPrimes(self, n: int) -> int:
# NOTE: Method is called "Sieve of Eratosthenes"
# Create a list
isNumPrime = [True] * n
# Walk through numeric system until sqrt(n)
# NOTE: The products from 1 to sqrt(n) are the same as sqrt(n) to n
# A while-loop was choosen because of condition for i
i = 2
while i*i < n:
# Check if element at ith position is a prime number
if isNumPrime[i]:
# Define every multiple of i as a non-prime number
j = i*i
while j < n:
isNumPrime[j] = False
j += i
# Increment i
i += 1
# Traverse list and count how many prime numbers are left
counter = 0
for i in range(2, len(isNumPrime)):
if isNumPrime[i]:
counter += 1
return counter