0

Hi all I have a question im working on in python and seem to be stuck on step 3 and 4. Any help would be great. This is the question:

Write a program which implements a function for the sieve of Eratosthenes. (http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes). The sieve of Eratosthenes is a simple algorithm for finding all prime numbers up to a specified integer. It was created by the ancient Greek mathematician Eratosthenes. The algorithm to find all the prime numbers less than or equal to a given integer n:

  1. Create a list of integers from two to n: 2, 3, 4, ..., n
  2. Start with a counter i set to 2, i.e. the first prime number
  3. Starting from i+i, count up by i and remove those numbers from the list, i.e. 2*i, 3*i, 4*i, and so on...
  4. Find the first number of the list following i. This is the next prime number.
  5. Set i to the number found in the previous step
  6. Repeat steps 3 and 4 until i is greater than n.
  7. All the numbers, which are still in the list, are prime numbers

This is what I have coded so far just don't know how to get the prime numbers from the list and remove the others....:

def main():    
    n = int(input("Enter a limiting number to find the prime numbers smaller than it: "))

    num_list = []
    num = 1
    for i in range(2, n + 1):
        num = num + 1
        num_list.append(num)

    i = 2
    for p in range(2, n):
        non_prime = num * i
        #while non_prime < n:
        if non_prime == num:
            num_list.remove(num)
    i = i + 1

    print(num_list)

main() # Call the main function

Thank for your help im banging my head against the wall as we speak.

Caleb
  • 120,112
  • 19
  • 171
  • 259
  • related: [Fastest way to list all primes below N in python](http://stackoverflow.com/q/2068372/4279) – jfs Mar 06 '14 at 21:15

2 Answers2

1

This is a sample implementation of the algorithm as explained on wikipedia:

limit = 100
numbers = [_ for _ in range (2, limit + 1) ]
primes = []

while numbers:
    candidate = numbers [0]
    primes.append (candidate)
    for i in range (candidate, limit + 1, candidate):
        if i in numbers: numbers.remove (i)

print (primes)

Explanation:

  • numbers is initialized with a list comprehension, containing 2 up to limit.

  • range (a, b, c) creates numbers starting from a until b (exclusive) in steps of c.

Thanks to erewoks input, here a more verbose version:

limit = 100
numbers = [_ for _ in range (2, limit + 1) ]
primes = []

while numbers:
    candidate = numbers [0]
    primes.append (candidate)
    factor = 1
    product = factor * candidate
    while product <= limit:
        if product in numbers: numbers.remove (product)
        factor += 1
        product = factor * candidate

print (primes)
Hyperboreus
  • 29,875
  • 7
  • 42
  • 78
0

One of the problems about using remove is that the main advantage of the sieve is that you don't have to go searching for the number in the list. One may code a sieve as follows

class Sieve(object):
    def __init__(self, limit):
        # step 1
        self.seive = [1] * limit
        # step 2
        self.index = 1
    def __next__(self):
        # step 4-5
        self.index += 1
        while self.index < len(self.seive) and not self.seive[self.index]:
            self.index += 1
        # step 6
        if self.index == len(self.seive):
            raise StopIteration
        # step 3
        for i in range(self.index ** 2, len(self.seive), self.index):
            self.seive[i] = 0
        return self.index
    def __iter__(self):
        return self
print(list(Sieve(100)))

The list is self.seive=[1] * limit (step 1), then we set self.index=1 (step 2), we increment it later, so it will start on 2.

The __iter__ methods does steps 3-5, although technically speaking we start with step 5 to find the next prime we haven't crossed out. Then we cross out all numbers that have that factor.

MWB
  • 171
  • 6