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
  Improve Prime Pairs Code jzakiya 7 1,640 Mar-09-2025, 08:19 PM
Last Post: jzakiya
  Complex Numbers Curbie 4 2,458 Feb-18-2025, 09:49 PM
Last Post: bowlofred
  [split] Prime numbers saima 1 584 Dec-09-2024, 02:19 AM
Last Post: jefsummers
  need help writing a program to run VIN numbers, any advice? Jhins007 4 1,251 Aug-23-2024, 08:06 AM
Last Post: DeaD_EyE
  python code to calculate mean of an array of numbers using numpy viren 3 1,269 May-29-2024, 04:49 PM
Last Post: Gribouillis
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 21,249 Jan-05-2024, 08:30 PM
Last Post: sgrey
  random numbers, randint janeik 2 1,680 Nov-27-2023, 05:17 PM
Last Post: janeik
  Prime number detector Mark17 5 2,324 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  Advancing Page Numbers knight2000 4 1,928 May-24-2023, 09:14 AM
Last Post: knight2000
  Pulling Specifics Words/Numbers from String bigpapa 2 1,663 May-01-2023, 07:22 PM
Last Post: bigpapa

Forum Jump:

User Panel Messages

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