Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
making primary numbers
#1
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
Reply
#2
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(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!
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 571 Aug-22-2023, 10:14 AM
Last Post: GYKR
  python sqlite autoincrement in primary column janeik 6 1,137 Aug-13-2023, 11:22 AM
Last Post: janeik
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,707 May-09-2019, 12:19 PM
Last Post: Pleiades

Forum Jump:

User Panel Messages

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