-2

I am new to Python and I'm attempting to write a code that checks to see whether or not a number is prime. So far, I've written this:

def main():
     n = input("Enter number:")
     isprime(n)

def isprime():
    x = Prime
    for m in range (1,n+1,1)
        result = n % m
        print result


main()

I'm not sure how to proceed after this. I know I have to define isprime. Any help would be greatly appreciated. Thank you!

  • Do you know how to do it on paper and you're just having problems putting it into code? Or do you not know how to do it at all? What is `x = Prime`? – takendarkk Nov 24 '14 at 22:28
  • @Takendarkk I'm unsure how to do it on both paper and code. I guess X = Prime is supposed to be the message when it says it's True (meaning the number entered is prime). – Mantis Tobaggan Nov 24 '14 at 22:30

2 Answers2

2

Your first and biggest problem is that you need to list n as a parameter to isprime before you can use it within isprime, and before you can pass an argument in main. See the tutorial on Defining Functions for more details. But basically, it's like this:

def isprime(n):

Also, that x = Prime is going to raise a NameError, because there's nothing named Prime. Given that it doesn't actually do anything at all, you should just delete it.

Of course that won't make this a complete working prime testing function, but it is how to proceed from where you are.


The next step is to consider what to return from the function. If you find a value that divides n, then obviously n isn't prime, and therefore isprime is false. If you go through all possibilities and don't find anything that divides n, then isprime is true. So, with two return statements in the right places, you can finish the function.


Once it's at least always returning True or False, you have bugs to fix.*

Look at what numbers you get from range(1, n+1, 1). Two of those numbers are guaranteed to divide any n. How do you avoid that problem?


After you've got it working, then you can work on optimizing it. If you look up primality test on Wikipedia, you can see a really simple way to improve the naive trial division test. A bit of research will show the pros and cons of different algorithms. But if what you've got is fast enough for your purposes, it's usually not worth putting more effort into optimizing.


* You might want to consider writing a test program that calls isprime on a bunch of numbers and compares the results with the right answers (for answers you already know off the top of your head—1 is not prime, 2 is prime, 17 is prime, etc.). This is called Test Driven Development, and it's a great way to make sure you've covered all the possible cases—including outliers like 0, 1, 2, -3, etc.

abarnert
  • 354,177
  • 51
  • 601
  • 671
1

Your isprime is quite close. The first line x = Prime is unnecessary, so let's get rid of that.

Now, let's take a look at what you're doing next:
You seem to want to check if there are any numbers that divide n perfectly. While this is the correct approach, you are including n and 1 in the numbers that you are testing. This is wrong for obvious reasons.

There is another error: you use n without ever defining it (I think you want it to be a parameter to your function). So let's put all this together:

def isprime(n):
    for m in range(2, n):
        if not n%m:  # m divides n perfectly
            return False
    return True  # if n were not prime, the function would have already returned

But going a step further, you don't really have to check every number between 2 and n-1. You just need to check the numbers between 2 and the square-root of n:

def isprime(n):
    for m in range(2, int(n**0.5)+1):
        if not n%m:
            return False
    return True
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241