Python Forum
prime numbers generator is generating non prime numbers? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: prime numbers generator is generating non prime numbers? (/thread-1254.html)



prime numbers generator is generating non prime numbers? - ixaM - Dec-18-2016

Hey guys. I wanted to create a prime numbers generator. This is how the program looks at the moment:
from decimal import *
PRIMES = [2]
p = 3
n = 2 
x = int(input("Until which number do you want to know the primes?"))
while True:
    if p/n == int(p/n) and p/n != 1.0:
        p = p+1
    if p/n != int(p/n):
        n = n+1
    if p/n == 1:
        PRIMES.append(p)
        p = p+1
        n = 2
    if p > x:
        break
print(PRIMES)
the program works okay, but it says, that some numbers are primes, which are just no primes. (For example 27)
So I would love it, if you could have a look for the program and tell me what mistake I have made.

Greets, ixaM

PS: You may have found some vocabulary or grammaticly mistakes in the text. Keep them with u :) I am not from an englishspeaking country, so yeah, my english is kinda bad. :(


RE: prime numbers generator is generating non prime numbers? - Yoriz - Dec-18-2016

The logic of you code means at the point that p is 27 , n is also 27, so it is appended to the list as p divided by n is 1
See this link to see the visualisation of the code at this point, move the slider to step back and forward through the code and see the variable values.

Note at step 511, p is 26 & n is 13, at the next step p is 27 & n is 13, so 3 an 9 divisors are missed out.


RE: prime numbers generator is generating non prime numbers? - ixaM - Dec-18-2016

Ah okay, thank you so much :) It is working now!