From 0f5479e5a302ba534bcd7bcc203fdbdfd259c27b Mon Sep 17 00:00:00 2001 From: Vinay Reddy Date: Sat, 7 Oct 2023 21:36:23 +0530 Subject: [PATCH] Create Sieve_of_Eratosthenes.py --- Sieve_of_Eratosthenes.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Sieve_of_Eratosthenes.py diff --git a/Sieve_of_Eratosthenes.py b/Sieve_of_Eratosthenes.py new file mode 100644 index 0000000..256b0dd --- /dev/null +++ b/Sieve_of_Eratosthenes.py @@ -0,0 +1,20 @@ +def sieve_of_eratosthenes(limit): + # Step 1: Create a list (sieve) of Boolean values initialized to True. + sieve = [True] * (limit + 1) + + # Step 2: Set the first two elements (0 and 1) to False since they are not prime. + sieve[0] = sieve[1] = False + + # Step 3: Iterate through the numbers starting from 2 up to the square root of the limit. + for i in range(2, int(limit**0.5) + 1): + # Step 4: If the current number (i) is marked as prime, mark its multiples as not prime. + if sieve[i]: + for j in range(i**2, limit + 1, i): + # Mark multiples of i as not prime by setting their values to False. + sieve[j] = False + + # Step 5: Collect the prime numbers (marked as True) into a list. + primes = [i for i in range(2, limit + 1) if sieve[i] + + # Step 6: Return the list of prime numbers. + return primes