Nov-07-2022, 08:08 PM
(Nov-06-2022, 02:45 AM)deanhystad Wrote: 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")
i didn't get why in the line
for x in range(3, int(user_input**0.5)+1, 2):
there is +1 ...