From 894a441c284a672fd1d3b02f43e0cf3d7a032f22 Mon Sep 17 00:00:00 2001 From: Khush3211 <34205476+khush3211@users.noreply.github.com> Date: Sat, 17 Oct 2020 17:23:39 +0530 Subject: [PATCH] Added to CountPrimesUsingSieve.py --- CountPrimesUsingSieve.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 CountPrimesUsingSieve.py diff --git a/CountPrimesUsingSieve.py b/CountPrimesUsingSieve.py new file mode 100644 index 0000000..a0151df --- /dev/null +++ b/CountPrimesUsingSieve.py @@ -0,0 +1,14 @@ +# Python Sieve of Eratosthenes - https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes +# The idea of the sieve of Eratosthene is to start with the list of all integer starting at 2 up to the limit we want to compute. +# A prime number is not the multiple of any number except 0, 1 and itself. +# So a way to find all primes up to the limit is to remove all multiples of the primes we currently know of starting at 2. +class Solution: + def countPrimes(self, n: int) -> int: + candidates = list(range(2,n)) + for idx, num in enumerate(candidates): + if num is not None: + for multiple in range(idx+num, len(candidates), num): + candidates[multiple] = None + return sum(1 for i in candidates if i is not None) + +# This algorithm is good if you want preprocessed data of prime numbers. #SharingKnowledge #Hacktoberfest2020