Python Forum
prime numbers - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: prime numbers (/thread-38604.html)

Pages: 1 2 3


RE: prime numbers - deanhystad - Nov-05-2022

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.


RE: prime numbers - astral_travel - Nov-05-2022

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")



RE: prime numbers - Yoriz - Nov-05-2022

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")



RE: prime numbers - deanhystad - Nov-05-2022

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


RE: prime numbers - astral_travel - Nov-05-2022

(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")



RE: prime numbers - Yoriz - Nov-05-2022

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.


RE: prime numbers - astral_travel - Nov-05-2022

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 ?


RE: prime numbers - Yoriz - Nov-05-2022

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?


RE: prime numbers - deanhystad - Nov-06-2022

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.


RE: prime numbers - deanhystad - Nov-06-2022

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")