Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

One way to find prime numbers less than n is to treat them as array indices. Mark each position as True initially, assuming that all

One way to find prime numbers less than n is to treat them as array indices. Mark each position as True initially, assuming that all numbers are prime. Then, starting with index 2, mark all multiples of 2 (greater than 2) as not prime (False). Repeat this for multiples of 3, then multples of 4, etc. When the algorithm terminates, only prime indices will still be True; everything else will be False.
The following function takes an integer n as a parameter and returns an array of prime integers less than n. Note that in the final step, we use list comprehension to fetch the indices whose values are True and place those indices as values in a new list. All displayed code is correct. Fill in the missing components, using spaces only where Python requires them (the minimum possible spacing).
def filter(n):
if n<3:
return []
primes=[True for i in range(n)]
primes[0]=False
i=
while i<:
while iand
is False:
i=
for j in range():
primes[j]=
i=
return [i for i in range(2,n) if primes[i] is True]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions