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,436 Jan-05-2024, 08:30 PM
Last Post: sgrey
  random numbers, randint janeik 2 526 Nov-27-2023, 05:17 PM
Last Post: janeik
  Prime number detector Mark17 5 735 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 723 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,427 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,012 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  prime numbers with iterator and generator cametan_001 8 1,771 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  List of random numbers astral_travel 17 2,531 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,251 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