Question

In: Computer Science

Here is a (buggy) version of the Eratosthenes sieve: # should return a list of prime...

Here is a (buggy) version of the Eratosthenes sieve:

# should return a list of prime numbers from 1..n (including n)

def sieve(n):

if n < 2: #there are no prime numbers < 2

return []

primes = [2] # 2 is the first prime number

for i in range(n): #put all of the numbers into primes initially

primes.append(i)

for current in range(n):

# start at current, and remove all multiples of current up to n

for j in range(current,n,current): primes.remove(j)

return primes

print(sieve(20))

Using PyCharm, under lab8, under Directory exercise, create a Python file called erat.py, and add the function above to it. Test the function with print(sieve(20)), as shown above.

Pycharm allows you to run programs in debug mode so that you can see the value of variables. First, we need to decide where we want to start looking, maybe the for current loop, the outer loop. In my code, that is on line 8. In the Pycharm editor window, go to the grey area to the right of the line number with the outer for loop, and right click. A red circle should appear. This is called a breakpoint. When you debug your program, the program will stop before executing this statement. To start debug, right click in the code area, and choose Debug instead of Run:

The program should begin running. A debug window should appear in the lower part of your PyCharm window that shows you the value of your variables. It should look something like this:

The variable i is set from the previous loop. primes is not filled with the correct values, but n is correct.

When debugging, you can continue running the program, or stop. To stop, press the red square. Don’t do that now (if you did, just debug again, and get back to the screen above). Instead, perform the next step, in the program by clicking the “Step Into My Code” icon:

current should appear with a value of 0, and the line number to be executed next should be 9 now. Play around with these buttons to see what happens as you execute the sieve function.

There could be other problems, but one problem we have already noticed is that primes is filled with incorrect values. This happened in the first for loop. Before the nested for loops, primes should be [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]. Use the debugger to figure out what went wrong, and fix that problem.

Instead of using the debugger, you can also add print statements to determine the values of the variables. Make sure you label all of the values, or it will be difficult to determine what values are being printed. For example, you might be the following inside the inner loop:

print('right now, current is', current, 'j is ', j, 'primes is', primes)

print('removing', j)

to see the value of the variables as you loop through.

Debug sieve, fix it, and run the tests for sieve. You will need to change the values within range statements, and add an if statement to make the code run correctly. You should not need to change anything else. Test your solution, and submit it.

Solutions

Expert Solution

Code screenshot:

erat.py

# should return a list of prime numbers from 1..n (including n)
def sieve(n):
   if n < 2: #there are no prime numbers < 2
       return []
   primes = [2] # 2 is the first prime number
   for i in range(3,n+1): #put all of the numbers into primes initially
       primes.append(i)
   for current in primes:
       # start at current, and remove all multiples of current up to n
       for j in range(2*current,n+1,current):
           if j in primes:
               primes.remove(j)
   return primes
print(sieve(20))

output screenshot:


Related Solutions

The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to...
The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit,” Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n - 1, whether the number is prime. In Java
Scenario Implementing the sieve of Eratosthenes algorithm to find all prime numbers up to a given...
Scenario Implementing the sieve of Eratosthenes algorithm to find all prime numbers up to a given limit. Aim Develop code for implementing the sieve of Eratosthenes. Steps for Completion Implement the isPrime() method of the SieveOfEratosthenes class that should return true if the number is prime, and false otherwise. Consider building the sieve in the class constructor. CODE GIVEN public class SieveOfEratosthenes { public SieveOfEratosthenes(int maxValue) { // Build the sieve here } public boolean isPrime(int value) { // Write...
IN PYTHON Use the sieve of Eratosthenes to find all primes less than 10,000.
IN PYTHON Use the sieve of Eratosthenes to find all primes less than 10,000.
C Program only - MUST USE MALLOC IN CODE Research and implement the Sieve of Eratosthenes....
C Program only - MUST USE MALLOC IN CODE Research and implement the Sieve of Eratosthenes. Example Program Session (implement some linefeed ‘\n’ formatting): Enter the limit: 1000 Primes up to 1000    2    3    5    7   11   13   17   19   23   29   31   37   41   43   47   53 59   61   67   71   73   79   83   89   97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211...
Consider this prime sieve in which an array of numbers of size N is created (i.e....
Consider this prime sieve in which an array of numbers of size N is created (i.e. int nums[10] = {2,3,4,5,6,7,8,9,10,11};) and initialized to contain counting numbers starting with 2. The sieve works by taking each number in the array, and “crossing out” (perhaps via a similar array of Booleans) multiples of that number. So beginning with 2, we would “cross out” 4, 6, 8, 10. Then repeat the process for 3, and so on. Numbers that are already “crossed out”...
Suppose when searching for a large prime, our first step is to sieve by eliminating the...
Suppose when searching for a large prime, our first step is to sieve by eliminating the first n primes. How large should n be to speed up our search by a factor of 10? This requires careful thought and inventiveness before doing a computation. How to approach this: Eliminating all odd numbers speeds up our program by a factor of 2, since we've eliminated half of the choices that are composite. If we also immediately disqualify multiples of 3, then...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of Eratosthenes method. You can find many articles that describe the method for finding primes in this manner on the Internet. Display all the prime values. This program should be in assembly language.
/** * This class maintains an arbitrary length list of integers. * * In this version:...
/** * This class maintains an arbitrary length list of integers. * * In this version: * 1. The size of the list is *VARIABLE* after the object is created. * 2. The code assumes there is at least one element in the list. * * This class introduces the use of structural recursion. * * @author Raymond Lister * @version May 2016 * */ public class ListOfNVersion03PartB {    private int thisNumber; // the number stored in this node...
/** * This class maintains an arbitrary length list of integers. * * In this version:...
/** * This class maintains an arbitrary length list of integers. * * In this version: * 1. The size of the list is fixed after the object is created. * 2. The code assumes there is at least one element in the list. * * This class introduces the use of loops. * * @author Raymond Lister * @version September 2015 * */ public class ListOfNVersion02PartB {    public int[] list; // Note: no "= {0, 1, 2, 3}"...
A number is prime if it can be divided exactly only by 1 and itself. Here...
A number is prime if it can be divided exactly only by 1 and itself. Here is an algorithm for checking if an input number is prime: function out = isprime(m)             for j:=2 to m-1             if mod(m,j) ==0 then             return “input is not a prime”             endif             endfor             return ”input is a prime” Let n denote the size of the input m, i.e. the number of bits in the binary expression for m. What is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT