hi
I was trying to print prime numbers from 3 to 100
this is my code
i=3
n=2
while i<100 :
while n < i :
if i%n == 0:
i+=1
else:
n+=1
if n==i:
print(i)
i+=1
n=2
else:
n=2
i+=1
the result is :3,5,7,11,13,17,19,23,
27,29,31,
35,37,41,...
I'm not looking for a "better code" or "how to make prime numbers" I just want to know why does it show numbers like 27 and 35 which are not prime
(Apr-26-2019, 09:44 AM)akbarasghar Wrote: [ -> ]I'm not looking for a "better code" or "how to make prime numbers" I just want to know why does it show numbers like 27 and 35 which are not prime
You set very strict limits what kind of help you need. Within those boundaries answer is simple: these numbers show up because you wrote such a code.
As you are not interested in 'making prime numbers' nor 'better code' which delivers prime numbers then nothing more can be done.
(Apr-26-2019, 10:19 AM)perfringo Wrote: [ -> ] (Apr-26-2019, 09:44 AM)akbarasghar Wrote: [ -> ]I'm not looking for a "better code" or "how to make prime numbers" I just want to know why does it show numbers like 27 and 35 which are not prime
You set very strict limits what kind of help you need. Within those boundaries answer is simple: these numbers show up because you wrote such a code. As you are not interested in 'making prime numbers' nor 'better code' which delivers prime numbers then nothing more can be done.
lol I mean I like to know why my code is wrong, it gives correct numbers form 3 to 23 but It shouldn't give 27. I tested it for i=27 and removed the loop it doesn't print 27 but when I use the loop it counts the 27 (also 35) as a prime number. I spend hours and couldn't find my mistake. does anybody know where is my mistake?
I don't need a new code I like to find my mistake
found the problem!
Your inner while loop continues on, and n is increased, not set to 2, when a not prime is found. You should definitely learn to be able to debug a simple program like this yourself. Add a print statement under the inner while to print the values, and add a print statement to tell you when you exit this while.