Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
prime numbers
#11
Why check if user input > 0? There is nothing special about 0.

Your checks for < 2 and == 2 are in the wrong place.

The for loop is wrong.

You are back to checking lots of factors that don't need checking.

Do some research on the for loop. It has a feature that could be useful. Should read about range too while you're at it.
Reply
#12
okay, i put it like this (at the beginning) and it works, but why didn't it work in the previous version ? (in the previous reply),

import sys

user_input = int(input("choose a number: "))

if user_input < 2:
    print("this number is not prime")
elif user_input > 0:
    for x in range(2, user_input-1):
        if user_input % x != 0:
            continue
        elif user_input % x == 0:
            sys.exit("number is not prime")
    print("number is prime")
elif user_input == 2:
    print("the number is prime")
Reply
#13
see comments
user_input = 1
if user_input > 0: # this if statement will be True
    for x in range(2, user_input-1): # this loop won't do anything
        if user_input % x != 0:
            continue
        elif user_input % x == 0:
            sys.exit("number is not prime")
    print("number is prime") # this will print
elif user_input == 2: # wont happen as the first if is True
    print("the number is prime")
elif user_input < 2:  # wont happen as the first if is True
    print("this number is not prime")
Reply
#14
I think frustration is making you go code blind. For example:
if user_input < 2:
    print("this number is not prime")
elif user_input > 0:
For the program to reach "elif user_input > 0:" you know that user_input cannot be less than 2. Why test if > 0 when you already know that the number has to be >= 2?

Why are checking if "user_input % x != 0"? We don't care if x is not a factor of user_input. We only care if x is a factor of user_input.
    for x in range(2, user_input-1):
        if user_input % x != 0:
            continue
        elif user_input % x == 0:
            sys.exit("number is not prime")
Your check for == 2 is still in the wrong place.

sys.exit is not meant for this purpose. It is a sloppy way to get around a fairly simple problem that you haven't figured out how to solve.
Use this as a skeleton to start your program.
# No imports
if user_input < 2:
    pass
elif user_input == 2:
    pass
else:
    pass
Replace the code where it says "pass
Reply
#15
(Nov-05-2022, 01:48 PM)deanhystad Wrote: Why check if user input > 0? There is nothing special about 0.

Your checks for < 2 and == 2 are in the wrong place.

The for loop is wrong.

You are back to checking lots of factors that don't need checking.

Do some research on the for loop. It has a feature that could be useful. Should read about range too while you're at it.

okay, is there some certain place/source where i can read about it and have what you want me to know ?
there are many websites but most of them lack the entirety of the possible information....
well, i don't know if i got what you implied me to,
this is what i wrote now, it works for every number but 3 (i don't know why),

i'm really sorry i'm not sure what to do

user_input = int(input("please choose a number: "))

if user_input < 2:
    print("number is not prime")
elif user_input == 2:
    print("number is prime")
else:
    for x in range(2, user_input - 1):
        if user_input % x == 0:
            print('number is not prime')
        else:
            print("number is prime")
Reply
#16
Like I did above with a user_input of 1, go through all the steps of this latest code with a user_input of 3 to decide what happens at every stage that user_input is referenced.
Reply
#17
yea i just got it while you were writing this,

the range is (2, user_input - 1) so when the user_input is 3 - it makes it -1 (to 2) when checking - and this overlaps/collides with the if statement from above (when comparing if user_input == 2),

am i correct ?
how can this be resolved ?
Reply
#18
You are correct that it makes a range(2, 2) but this does not affect the line elif user_input == 2:, that line has already been passed and was False.
As all the if where False the code in the else: code happened.
The first line in the else block would have been for x in range(2, 2):
What does a range that's stop is less than or equal to its start do?
Reply
#19
This is still wrong.
    for x in range(2, user_input - 1):  # I don't like the range, but not an error
        if user_input % x == 0:  # This is correct
            print('number is not prime')
        else:  # This is wrong
            print("number is prime")
user_input % x != 0 is not enough to know that the number is prime. You have to test every factor before you can say the number is prime.

Research the for loop. It has a feature that would be useful for this program.
Reply
#20
Sorry, I gave bad advice. The test for == 2 should be done first. Then I can add a test for even that makes it easier to only test odd factors.

I would write like this:
while True:
    user_input = int(input("Enter Number "))

    if user_input == 2:   # Test first
        print("is prime")
    # No number < 2 is prime.  No even number, other than 2, is prime.
    # Doing this test 2nd lets me report that 2 is prime.
    elif user_input < 2 or user_input & 1 == 0: 
        print("is not prime")
    else:
        # Test all odd numbers between 3 and square_root(user_input)
        for x in range(3, int(user_input**0.5)+1, 2):
            if user_input % x == 0:
                print('is not prime')
                break  # Found a factor.  No reason to test any more factors
        else:
            # If no factor found, must be prime
            print("is prime")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  prime numbers with iterator and generator cametan_001 8 1,975 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  Return prime numbers from range krzyfigh 2 1,975 Apr-20-2020, 08:08 PM
Last Post: krzyfigh
  Prime numbers Anderi02 1 2,011 Oct-13-2019, 04:49 PM
Last Post: ichabod801
  first k non-prime numbers arycloud 11 7,481 Jul-09-2019, 02:19 PM
Last Post: abhi19935
  first k non prime numbers print bsrohith 7 7,651 Jun-20-2019, 10:48 AM
Last Post: arycloud
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,808 May-09-2019, 12:19 PM
Last Post: Pleiades
  Finding prime numbers creslin_black 7 4,449 Jul-20-2018, 02:28 PM
Last Post: grjmmr
  Prime Numbers OmarSinno 1 4,416 Sep-23-2017, 05:29 PM
Last Post: ichabod801
  prime numbers generator is generating non prime numbers? ixaM 2 4,513 Dec-18-2016, 01:35 PM
Last Post: ixaM

Forum Jump:

User Panel Messages

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