Reference the algorithm in the attched image. (a) Write code to implement the algorithm in python. (b) Show the output for Sieve(60) and Sieve(200) (c) Modify the code to count the number of times the if statement on line 3 is executed. (d) Show the output for Sieve (160) including the output for part (c) above

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Reference the algorithm in the attched image.
(a) Write code to implement the algorithm in python.
(b) Show the output for Sieve(60) and Sieve(200)
(c) Modify the code to count the number of times the if statement on line 3 is executed.
(d) Show the output for Sieve (160) including the output for part (c) above

ALGORITHM Sieve(n)
//Implements the sieve of Eratosthenes
//Input: A positive integer n > 1
//Output: Array L of all prime numbers less than or equal to n
for p 2 to n do A[p] + p
for p +2 to √√ndo
if A[p] #0
j← p*p
while j≤ n do
A[j] 0 //mark element as eliminated
j+j+p
//copy the remaining elements of A to array L of the primes
i←0
for p +2 to n do
if A[p] #0
//see note before pseudocode
//p hasn't been eliminated on previous passes
L[i] + A[p]
i←i +1
return L
Transcribed Image Text:ALGORITHM Sieve(n) //Implements the sieve of Eratosthenes //Input: A positive integer n > 1 //Output: Array L of all prime numbers less than or equal to n for p 2 to n do A[p] + p for p +2 to √√ndo if A[p] #0 j← p*p while j≤ n do A[j] 0 //mark element as eliminated j+j+p //copy the remaining elements of A to array L of the primes i←0 for p +2 to n do if A[p] #0 //see note before pseudocode //p hasn't been eliminated on previous passes L[i] + A[p] i←i +1 return L
Expert Solution
Step 1

Algorithm:

  1. Initialize a count variable to 0 and create a list of n+1 elements with all elements set to True, where n is the input value.

  2. Loop through the range of 2 to the square root of n+1.

  3. For each iteration of the loop, check if the current value of the list is True. If it is, increment the count variable by 1 and perform the following steps.

  4. Initialize a variable j as the square of the current iteration number.

  5. While j is less than or equal to n, set the corresponding element in the list to False and increment j by the current iteration number.

  6. After the loop, initialize an empty list L.

  7. Loop through the range of 2 to n+1.

  8. For each iteration of the loop, check if the current value of the list is True. If it is, append the current iteration number to the list L.

  9. Return the list L and the count variable as the output of the function.

  10. Call the function with input values 60, 200, 50, and 160 and print the output.

steps

Step by step

Solved in 6 steps with 7 images

Blurred answer
Knowledge Booster
Matrix multiplication
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education