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

19th Jul 2019, 5:47 PM
Dawid Uzarski
Dawid Uzarski - avatar
4 Answers
+ 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)
19th Jul 2019, 7:48 PM
Paul
Paul - avatar
+ 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.
19th Jul 2019, 6:05 PM
Akash
Akash - avatar
+ 1
Ohh thx Paul Jacobs I miss that
19th Jul 2019, 9:06 PM
Dawid Uzarski
Dawid Uzarski - avatar
+ 1
Dawid Uzarski by version of finding primes in an given range. https://code.sololearn.com/csBVB3Rj8ttP/?ref=app
20th Jul 2019, 2:59 PM
Akash
Akash - avatar