From e89ed71b3ffb5881cb5bce4fcd7bb308032d6c28 Mon Sep 17 00:00:00 2001 From: Shubham Lad Date: Mon, 21 Oct 2019 18:48:43 +0530 Subject: [PATCH] added Sieve Of Eratosthenes Prime Number algorithm --- Python/SieveOfEratosthenesPrimeNumber.py | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Python/SieveOfEratosthenesPrimeNumber.py diff --git a/Python/SieveOfEratosthenesPrimeNumber.py b/Python/SieveOfEratosthenesPrimeNumber.py new file mode 100644 index 0000000..23193c0 --- /dev/null +++ b/Python/SieveOfEratosthenesPrimeNumber.py @@ -0,0 +1,29 @@ +''' +Sieve of Eratosthenes + +Input : n =10 +Output : 2 3 5 7 + +Input : n = 20 +Output: 2 3 5 7 11 13 17 19 +''' + +def primeNumbers(num): + + primes = [True for i in range(num + 1)] + p = 2 + + while p * p <= num: + if primes[p] == True: + for i in range(p*p, num+1, p): + primes[i] = False + p+=1 + + for prime in range(2, num+1): + if primes[prime]: + print(prime, end=" ") + +if __name__ == "__main__": + num = int(input()) + + primeNumbers(num) \ No newline at end of file