Python Forum
prime numbers generator is generating non prime numbers?
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
prime numbers generator is generating non prime numbers?
#1
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. :(
Reply
#2
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.
Reply
#3
Ah okay, thank you so much :) It is working now!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,448 Jan-05-2024, 08:30 PM
Last Post: sgrey
  random numbers, randint janeik 2 527 Nov-27-2023, 05:17 PM
Last Post: janeik
  Prime number detector Mark17 5 736 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  Advancing Page Numbers knight2000 4 933 May-24-2023, 09:14 AM
Last Post: knight2000
  Pulling Specifics Words/Numbers from String bigpapa 2 724 May-01-2023, 07:22 PM
Last Post: bigpapa
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,430 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,049 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  prime numbers with iterator and generator cametan_001 8 1,773 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  List of random numbers astral_travel 17 2,534 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,252 Nov-13-2022, 01:27 AM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020