Question: Please explain line by line, what the code below is doing. It's supposed to detect prime numbers using sieve of eratosthenes alg. Output = [2
Please explain line by line, what the code below is doing. It's supposed to detect prime numbers using sieve of eratosthenes alg. Output = [2 , 3, 5, 7] # Sieve of Eratosthenes: def all_prime_below_sieve (N): # initialize a list of numbers L = [] for i in range(N): L.append(i) # 0, 1 are not prime L[0] = 0 L[1] = 0 # start sieving i = 0 while (i < N): if (L[i] != 0): p = L[i] k = i + p while (k < N): L[k] = 0 k = k + p i = i + 1 # clean the list L2 = [] for i in range(N): if (L[i] != 0): L2.append(L[i]) return L2 print(all_prime_below_sieve (10))
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
