Question

In: Computer Science

Here is the algorithm: 1. Assume that all numbers are prime from 2..n, so put them...

Here is the algorithm:

1. Assume that all numbers are prime from 2..n, so put them in the list (2 is the smallest number, so if n is less than 2, the list should be empty).

2. Make the first item be current.

3. Leave in the current item. Then, take out all of its multiples (since any multiple will have that number as a factor).

4. Make current be the next element in primes.

5. If there are no more elements in the list, primes is the list of prime numbers from 2..n. Otherwise, go to Step 3

For example, sieve(20) would find the primes from 1 to 20. Step 1 assign primes to be [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Step 2 assigns 2 to current. current is always a prime number. Step 3 would remove from primes 4, 6, 8, 10, 12, 14, 16, 18 and 20 because they are multiples of 2. Then primes = [2, 3, 5, 7, 9, 11, 13, 15, 17, 19] (all of the original numbers except the evens > 2). current becomes the next element in the list, which is 3 (step 4). Because there are more items in the list, we go back to step 3. We take out 6, 9, 12, 15, 18 (multiples of 3); the even numbers, of course, are already out. primes is now [2, 3, 5, 7, 11, 13, 17, 19]. This is the list of prime numbers, but we keep going because there are more values in the list. current becomes 5, and we go back to step 3, and take out 10 and 15 (both of which are already out). current becomes 7, and we take out 14 (already out). We continue until current because 19. We are then out of numbers, so we stop, and return the list of primes from 2..20: [2, 3, 5, 7, 11, 13, 17, 19].

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))

Can you please fix this so it works properly without completely overhauling the code? (please keep the same type of code, just edit it so it is no longer broken) (Here is a screenshot of the properly spaced broken code: https://i.imgur.com/vjzFkRT.png)

Thanks!

Solutions

Expert Solution

There were a few changes I made to your code. I have listed them below :


1) Added an if statement at line 10 because your code was generating the wrong list. What your code did was create a list with 2 as the the first number and then all the numbers from 1 to n, we know this is wrong so thats why I added the if statement so that the generated list contains only numbers from 2 to n.

2) Changed the starting point of the inner loop at line 14 from current to current*current, because python throws an error if we try to delete an element from the list that is not present in it. Lets take the number 6 for example, this will be deleted when current = 2 as it is a multiple of 2 but when current = 3 python will not be able to find 6 in the list and will throw an error, thats why this change was necessary. The if statement inside also complements this.

BELOW IS THE MODIFIED CODE

# 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
                if i > 2:
                        primes.append(i)
        
        for current in range(2,n): # start at current, and remove all multiples of current up to n
                for j in range(current*current,n,current):
                        if primes.count(j):
                                primes.remove(j)
        
        return primes

print(sieve(20))
print(sieve(40))

OUTPUT


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...
Consider the statement, “For all natural numbers n,n, if nn is prime, then nn is solitary.”...
Consider the statement, “For all natural numbers n,n, if nn is prime, then nn is solitary.” You do not need to know what solitary means for this problem, just that it is a property that some numbers have and others do not. Write the converse and the contrapositive of the statement, saying which is which. Note: the original statement claims that an implication is true for all n,n, and it is that implication that we are taking the converse and...
Show that among all collections with 2n-1 natural numbers in them there are exactly n numbers...
Show that among all collections with 2n-1 natural numbers in them there are exactly n numbers whose sum is divisible by n.
Using HTML and JavaScript, receive a positive number, n, and output all prime numbers that are...
Using HTML and JavaScript, receive a positive number, n, and output all prime numbers that are smaller than n and have a digit 7. For example, if n is 100, the program should output 7, 17, 37, 47, 67, 71, 73, 79, and 97.
Find the biggest “*prime gap” (see definition) from the prime numbers between 1 and 1,000,000. *Prime...
Find the biggest “*prime gap” (see definition) from the prime numbers between 1 and 1,000,000. *Prime gap = the difference between two consecutive primes. The exercise can be completed manually or with a computer program. Whichever seems easiest.
Show by induction that if a prime p divides a product of n numbers, then it...
Show by induction that if a prime p divides a product of n numbers, then it divides at least one of the numbers. Number theory course. Please, I want a clear and neat and readable answer.
Show by induction that for all n natural numbers 0+1+4+9+16+...+ n^2 = n(n+1)(2n+1)/6.
Show by induction that for all n natural numbers 0+1+4+9+16+...+ n^2 = n(n+1)(2n+1)/6.
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.
(Prime Numbers) An integer is said to be prime if it is divisible by only 1...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write pseudocode and function called isPrime that receives an integer and determines whether the integer is prime or not. Write a test program that uses isPrime to determine and print all the prime numbers between 1 and 1000. Display 10 numbers per line. Twin primes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT