-1

I came across this prime sieve using the enumerate function which I vaguely understand, I understand the general idea behind the code, i.e. turning all the non-prime values in the list of 'True' to 'False' and then out of that creating a prime list. But I'm not exactly sure how to interpret the code after line 5, is is_prime a function or a variable? I'm slightly confused

def prime_sieve(n):
    sieve = [True] * n #Generates a list of 'True' of length n
    sieve[:2] = [False, False]  #  0 and 1 are not primes
    primes = []
    for prime, is_prime in enumerate(sieve): 
        if not is_prime:
            continue
        primes.append(prime)
        for not_prime in range(prime*prime, n, prime):
            sieve[not_prime] = False 
    return primes, sieve
Anonymous
  • 13
  • 1
  • `emumerate` returns tuples of `(index, item)` so `is_prime` is just the item value at the index `prime`, ie it is `True` or `False` depending on whether it is prime or not. – R Nar Dec 01 '17 at 20:33
  • "is is_prime a function or a variable?" Functions are just like any other object, and are assigned to *variables*, so I'm not sure what you mean, but it *is* a variable, one that doesn't happen to reference a function. – juanpa.arrivillaga Dec 01 '17 at 21:30

3 Answers3

2
def prime_sieve(n):
    sieve = [True] * n #Generates a list of 'True' of length n
    sieve[:2] = [False, False]  #  0 and 1 are not primes
    primes = []

Declares the function, sets the variables. Sieve is a list of booleans meant to represent whether the number of a given index is prime or not. Primes is set to an empty list of primes found so far.

    for prime, is_prime in enumerate(sieve): 

Iterate through an enumerated list of all numbers, and their boolean counterparts. Enumerate is like iterating over a list, but also having the index of all list variables. Since enumerate gives two variables for every list element, you need to use two variables to unpack it. 'prime' is going to be a number, the index of the list element being accessed, and 'is_prime' is the boolean value at that index.

        if not is_prime:
            continue

If a number has previously been declared to be 'not prime', then skip it.

primes.append(prime)

If not, add it to the list of prime numbers already found.

        for not_prime in range(prime*prime, n, prime):
            sieve[not_prime] = False 

Starting at the prime's square to the end, all numbers that are divisible by the prime are set to False

 return primes, sieve

Returns the list of all prime numbers, and the boolean list of all numbers in the sieve.

1

try this:

sieve = [True,True,True]
for x,y in enumerate(sieve):
   print(x,y)

Do you understand now?

1

If is_prime were a function, then it would not be False, so not is_prime would always return False, regardless of what is_prime is. Note that there's a difference between a function and a function return; if is_prime were a function, then is_prime() would be what is_prime() returns, which could be False, but is_prime would be the function object itself, which is not False. A function can return False, but it can't be False.

Acccumulation
  • 3,491
  • 1
  • 8
  • 12