bhuskinson
bhuskinson

Reputation: 5

What is the flaw with my logic in a python function for checking primes?

I'm relatively new to Python and trying to build a function to check primes because I thought it would be a good starter project, but my code returns everything as a prime, so it's obviously gone wrong. I get this is an inefficient way to do it, but I want to understand how to do it the long way first. Here's my code so far:

def Prime(n):
    if n == 1 or n == 2 or n == 3:
        print("This number is prime.")
    else:
        i = n - 1
        while i > 0:
            if n % i == 0:
                break
                print("This number is not prime.")
            else:
                i = i - 1
            print("This number is prime.")

def Main():
    n = int(input("What is the number you'd like to check?"))
    Prime(n)
    answer2 = input("Thank you for using the prime program.")

Main()

Upvotes: 0

Views: 54

Answers (4)

zxzxzx
zxzxzx

Reputation: 33

Ok, first off, your code will never print "This number is not prime." because you've put a break statement right before it. You would want to reverse those two lines such that it becomes:

print("This number is not prime.")
break

Also, the line i = n - 1 should be changed to i = n so that you check the starting number as well, and not just the numbers lesser than it. So if you try the code now, you'll notice that you're printing whether every number that you check is prime or not, not just the input value. To remedy this using your code structure, use a flag. For example:

    def Prime(n):
    flag = true
    if n == 1 or n == 2 or n == 3:
        print("This number is prime.")
    else:
        i = n
        while i > 0:
            if n % i == 0:
                print("This number is not prime.")
                break
            else:
                i = i - 1
        if flag == true:
            print("This number is prime.")

The flag check should be outside the loop.

Upvotes: 0

atru
atru

Reputation: 4744

Here is your program with a couple changes:

def Prime(n):
    if n == 1 or n == 2 or n == 3:
        print("This number is prime.")
    else:
        i = n - 1
        while i > 1:
            if n % i == 0:
                print("This number is not prime.")
                return
            i = i - 1
        print("This number is prime.")
        return

def Main():
    n = int(input("What is the number you'd like to check? "))
    Prime(n)
    print "Thank you for using the prime program."

Main()

First, i is now compared until i > 1 rather than 0 as every number is divisible by 1 and hence all numbers would be prime if the original condition was used.

Secondly, the break statement is substituted with return. Although break would work, the program would need more modifications in that case because the message for a prime number would always be printed at the end (along with not a prime message). Also, the return statement was moved after the print to actually get a print. More importantly, the message that a number is a prime was moved outside the while - otherwise the message would be printed on every iteration.

I also removed the else and the i is decremented right in the while loop which is to me a more readable alternative.

Finally, your last statement was, I assume, supposed to be an output, which it is now. I also added a space to the user prompting message so that the number displays more nicely.

Upvotes: 0

rachid
rachid

Reputation: 2496

Mathematically speaking you could check only all the int between 0 and sqrt(n) to judge a number is prime or not as for the logic, you are missing negative number handling plus other things please see my following code:

def prime(n):
    n = abs(n)
    if n<4: return True
    i = int(sqrt(n))
    while i > 1:
        if n % i == 0: return False
        i -= 1
    return True

plus you should add this to your import

from math import sqrt

Upvotes: 1

Aaron
Aaron

Reputation: 2393

It's not easy to explain the logic flaw, but you can see the following code

def Prime(n):
    if n == 1 or n == 2 or n == 3:
        print("This number is prime.")
    else:
        i = n - 1
        while i > 0:
            if i == 1 or n == 2 or n == 3:
                print("This number is prime.")
                break
            if n % i == 0:
                print("This number is not prime.")
                break
            else:
                i = i - 1

def Main():
    n = int(input("What is the number you'd like to check? "))
    Prime(n)
    print("Thank you for using the prime program.")

Main()

Upvotes: 0

Related Questions