Check if a Number Is Prime in Python

  1. Use the Simple Iteration Method to Determine a Prime Number in Python
  2. Use the sympy.isprime() Function to Check if the Given Number Is a Prime Number in Python

A prime number can be depicted as a natural number with no other positive divisors, except for the number 1 and itself. The number 1 is not counted in the list of primes.

This tutorial will discuss the different methods you can use to check whether a number is a prime number.

Use the Simple Iteration Method to Determine a Prime Number in Python

In this method, we use a simple iteration method using a for or while loop. Iterate through the numbers starting from 2 and going up until K/2 and checking if any of these numbers divide K.

If there is a number found that matches this criterion, then False is returned. On the other hand, if all the numbers fail to match this criterion, the given number K is a prime number, and the True value is returned.

The following code uses the simple iteration method to check whether the given number is a prime number in Python.

k = 13

# 1 not being a prime number, is ignored
if k > 1:
    for i in range(2, int(k/2)+1):
         if (k % i) == 0:
            print("It is not a prime number")
            break
    else:
        print("It is a prime number")

else:
    print("It is not a prime number")

Output:

It is a prime number

You can optimize the code above by applying some changes. Do the following optimizations to make the code even faster:

*Check until the root of the given number is reached instead of checking for the exact number. This process is basically eliminating redundancy that occurs when a larger factor of the number K is a multiple of a smaller factor that has already been iterated over.

*All primes exist in the form of 6n±1, with 2 and 3 being the only exceptions. Therefore, checking the divisibility of the given number with 2 and 3, and then checking through every number that has the form 6n±1 is the more efficient solution.

The following code uses the optimized simple iteration method to check whether the given number is a prime number in Python.

def isitPrime(k):
    if k==2 or k==3: return True
    if k%2==0 or k<2: return False
    for i in range(3, int(k**0.5)+1, 2):
        if k%i==0:
            return False

    return True
print(isitPrime(13))

Output:

True

The optimized iteration method makes it faster and more efficient than the simple iteration method by about 30%.

Use the sympy.isprime() Function to Check if the Given Number Is a Prime Number in Python

SymPy is a library in Python used for implementing Symbolic Mathematics. It aims to be a simplistic computer algebra system (CAS) that contains all the essential features. The installation of this module is necessary for this method, and it can be downloaded by simply using the pip command.

The sympy.isprime() is a built-in function under the SymPy module and can be utilized for checking of possible prime numbers. It is a direct function and returns True if the number to be checked is prime and False if the number is not prime.

The following code uses the sympy.isprime() function to check whether the given number is a prime number in Python.

from sympy import *
  
isprime(8)
isprime(11)

Output:

False
True

We should note that any negative number does not come under the criteria of Prime Numbers. The output of these functions may vary if any negative number is checked against it.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.