Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the following algorithm in Python with a function called sieve_of_eratosthenes ( n ), where n is an integer value, which returns all prime numbers

image text in transcribed

Implement the following algorithm in Python with a function called sieve_of_eratosthenes ( n ), where n is an integer value, which returns all prime numbers up to n. For example, >>> sieve_of_eratosthenes ( 30 ) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] See the following page for more: https://en.wikipedia.org/wiki/Sieve of Eratosthenes ALGORITHM Sieve_of_Eratosthenes ( n ): INPUT: An integer n > 1 OUTPUT: All prime numbers [2 .. n] LET A be an array of boolean values, indexed by integers [2 .. n] initially all set to True. FOR i = 2, 3, 4, ..., not exceeding Vn DO if A[i] is True: FOR j = i^2, i^2+i, i^2+2i, i^2+3i, ..., not exceeding n DO # Mark all numbers that are not prime numbers A[j] = False RETURN all i for which A[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

Students also viewed these Databases questions