+ 1
Python - prime numbers generator
Hello Why my code don't work? numb = int(input()) primeNumb = [2,] i = 3 b = True z = 2 while i < numb: while b == True: while z < i: if i % z == 0: b = False z = z + 1 print(primeNumb) primeNumb.append(i) i = i + 1 print(primeNumb)
4 Réponses
+ 2
You must reset z and b to its original value somewhere in the loops. I solved it like this:
while i < numb:
while (z < i and b == True):
if i % z == 0:
b = False
z = z + 1
z = 2 # reset z
if b == True:
primeNumb.append(i)
b = True # reset b
i = i + 1
print(primeNumb)
+ 1
Dawid Uzarski I think there is a logic error bcoz, let numb=9, i=8 and z=4, it satisfies all the above conditions and 8%4 is 0,
Therefore according to your code 8 is a prime number but it is not.
+ 1
Ohh thx Paul Jacobs I miss that
+ 1
Dawid Uzarski by version of finding primes in an given range.
https://code.sololearn.com/csBVB3Rj8ttP/?ref=app