简体   繁体   中英

Python: Verifying a prime number

i have written the following code which should check if the entered number is a prime number or not, but there is an issue i couldn't get through:

def main():
    n = input("Please enter a number:")
    is_prime(n)

def is_prime(a):
    x = True 
    for i in (2, a):
        while x:
            if a%i == 0:
                x = False
            else:
                x = True

    if x:
        print "prime"
    else:
        print "not prime"

main()

If the entered number is not a prime number, it displays "not prime", as it is supposed to. But if the number is a prime number, it doesn't display anything. Could you please help me with it?

There are many efficient ways to test primality (and this isn't one of them). But the loop you wrote can be concisely represented in Python:

def is_prime(a):
    return all(a % i for i in xrange(2, a))

That is, a is prime if all numbers between 2 and a (not inclusive) give non-zero remainder when divided into a.

There is a very nice trick where you only need to iterate up to the square root of the number you are testing for primality:

def is_prime(a):
    x = True 
    for i in range (2, int(a**(0.5))+1):
        if a % i == 0:
            x = False
            break

    if x:
        print "prime"
    else:
        print "not prime"

You can also optimize in other ways (check if the number is even before you even begin looping). If you are using Python2, use xrange instead of range etc.

After your line x = False add a line, at the same level of indentation, break . This will cause your function to immediately report a composite number as soon as it finds a factor.

There are better ways to determine if a number is prime or composite. If you are interested, I modestly recommend the essay Programming with Prime Numbers at my blog.

first of all you have

 for i in (2, a): 

instead of

 for i in xrange(2, a): 

secondly, you don't need while x. because if you give it any prime it will stuck there.

3rd is that you don't need to check if you found a non-prime, just print that it is not prime and return.

4th is that you really just need to check until a**(1/2)+1, because any number greater than that can't be small divisor.

here is somehow fixed code:

def main():
    n = input("Please enter a number:")
    is_prime(n)

def is_prime(a):
    for i in xrange(2, a**1/2):
        if a%i == 0:
            print "not prime"
            return
        else:
            continue

    print "prime"
    return

main()

This is a simple improvement on your code:

def is_prime(a):
    if a % 2 == 0:
        return a == 2
    for i in range (3, int(a ** 0.5) + 1, 2):
        if a % i == 0:
            return False
    return True

Or even:

def is_prime(a):
    if a % 2 == 0:
        return a == 2
    else:
        return all(bool(a % i) for i in range (3, int(a ** 0.5) + 1, 2))

Then some tests:

>>> for i in range(2, 25):
...     print i, is_prime(i)
... 
2 True
3 True
4 False
5 True
6 False
7 True
8 False
9 False
10 False
11 True
12 False
13 True
14 False
15 False
16 False
17 True
18 False
19 True
20 False
21 False
22 False
23 True
24 False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM