kouty 0 Junior Poster in Training

Hi my friends!
Here is a wrong code, Wrong and I don't know why.

My purpose is to make a sieve of Erasthotene.

I make an unique function with many inner loops.

But the execution don't go cross the lines
I illustrate this with the help of printing the different steps of the script.
If someone can poiint out some error or lack of understanding, he is welcome an I am grateful!
HERE IS THE CODE:

from math import sqrt
def holeofStrainer():
  bigList = [False, False] + [True]*100
  print("line 4 - bigList : ", bigList)
  for num in range(2, 101):
    print("line 6 - num : ", num)
    for x in range(bigList[2], bigList[int(sqrt(num)) + 1]):
      print("line 8 x : %d"%x)
      if num % x == 0:
        print("line 10 {0} divise par {1} = {2} ".format(num, x, num/x))
        bigList[num] == False
        print "bigList[{0} == {1}]".format(num, bigList[num])
      bigList[num] == True

    for multiple in range (2, int(101/num) + 1):
      bigList[multiple] = False
  return(bigList)
print("the last result of bigList {} ".format(holeofStrainer()))

Dani AI

Generated

In 's snippet the implementation mixes two different ideas (trial division and a marking loop) and has a few simple mistakes that stop the loop from making progress. The main problems are: using list values/booleans as range endpoints, using the comparison operator == where an assignment (=) was intended, and marking the wrong indices when crossing out multiples. The code also mixes Python 2 and 3 print styles, which can hide runtime behavior when run under a single interpreter.

Concretely:

  • range(bigList[2], bigList[int(sqrt(num)) + 1]) uses boolean/list values instead of numeric bounds; booleans become 0/1 but that is accidental and wrong for the intended loop.
  • bigList[num] == False compares instead of assigning, so it has no effect.
  • for multiple in range(2, int(101/num) + 1): bigList[multiple] = False touches the wrong indices; the multiples of num are at num * multiple, or better yet start marking at num*num and step by num.
  • The sieve is simpler and faster if the outer loop runs to sqrt(n) and the inner loop marks p*p, p*p+p, ....

A concise, correct Sieve of Eratosthenes (returns the prime numbers ≤ n):

from math import sqrt

def sieve(n):
    if n < 2:
        return []
    is_prime = [True] * (n + 1)
    is_prime[0] = is_prime[1] = False
    limit = int(sqrt(n)) + 1
    for p in range(2, limit):
        if is_prime[p]:
            for multiple in range(p * p, n + 1, p):
                is_prime[multiple] = False
    return [i for i, prime in enumerate(is_prime) if prime]

print(sieve(100))

Troubleshooting tips: run under a single Python version (prefer Python 3), try small n (e.g. 30) and print the boolean array briefly to confirm marking, and replace print debug with short assertions for expected primes (for example assert 97 in sieve(100)). This keeps logic clear and avoids the accidental behaviors seen in the original post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.