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
Reference the
(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:
-
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.
-
Loop through the range of 2 to the square root of n+1.
-
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.
-
Initialize a variable j as the square of the current iteration number.
-
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.
-
After the loop, initialize an empty list L.
-
Loop through the range of 2 to n+1.
-
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.
-
Return the list L and the count variable as the output of the function.
-
Call the function with input values 60, 200, 50, and 160 and print the output.
Step by step
Solved in 6 steps with 7 images