If you’ve ever taken up coding tests, you’ll have come across the math question on the test for primality or to check if a number is prime. And over the next few minutes, you’ll learn to come up with the optimal solution to this question. In this tutorial, you’ll:

review the basics of prime numbers,write Python code to check if a number is prime, and optimize it further to get an O(√n) runtime algorithm.

For all this and more, let’s get started.

What is a Prime Number?

Let’s start by reviewing the basics of prime numbers. The following table lists down a few numbers, all their factors, and if they’re prime. From the above table, we can write down the following:

2 is the smallest prime number.1 is a factor of every number.Every number n is a factor of itself.

So 1 and n are trivial factors for any number n. And a prime number should not have any factors other than these two. This means that when you go from 2 to n – 1, you should not be able to find a non-trivial factor that divides n without a remainder.

O(n) Algorithm to Check if a Number is Prime in Python

In this section, let us formalize the above approach into a Python function. You can loop through all numbers from 2 to n – 1 using the range() object in Python. Since we need the set of integers from 2 to n-1, we can specify range(2, n) and use it in conjunction with for loop. Here’s what we would like to do:

If you find a number that divides n evenly without a remainder, you can immediately conclude that the number is not prime.

If you’ve looped through the entire range of numbers from 2 all the way up to n – 1 without finding a number that divides n evenly, then the number is prime.

Python Function to Check for Prime Number

Using the above, we can go ahead and define the function is_prime() as follows. Let’s now parse the above function definition.

The above function is_prime() takes in a positive integer n as the argument.If you find a factor in the specified range of (2, n-1), the function returns False—as the number is not prime.And it returns True if you traverse the entire loop without finding a factor.

Next, let’s call the function with arguments and verify if the output is correct. In the output above, you see that 2 and 11 are prime (the function returns True). And 8 and 9 are not prime (the function returns False).

How to Optimize the Python Function is_prime()

We can do a small optimization here. Observe the list of factors in the table below. So this means you don’t have to loop all the way up to n – 1. You can instead run the loop only up to n/2. If you don’t find a non-trivial factor till n/2, you can’t find one beyond n/2 either. Now let’s modify the function is_prime() to check for factors in the range (2, n/2) Let’s go ahead and verify the output through a few function calls. Even though it may seem like we have optimized, this algorithm is not more efficient than the previous one in terms of runtime complexity. In fact, both of them have O(n) runtime complexity: proportional to the value of n or linear in n. Can we do better? Yes, we can! ▶️ Proceed to the next section to determine how to improve the time complexity for prime number testing.

O(√n) Algorithm to Check for Prime Number in Python

It happens that the factors of a number occur in pairs. Let’s verify this through an example. The table below shows the factors of the number 18 occurring in pairs. You may verify the same for a few more numbers if you like. Also, note that √18 is approximately 4.24. And in the factors that occur in pairs (a, b), you can see that if a is less than 4.24, then b is greater than 4.24—in this example, (2, 18) and (3, 6). The figure below shows the factors of 18 plotted on the number line. If the number n happens to be a perfect square, you’ll have a = b = √n as one of the factors. ▶️ Look at the factors of 36 in the table below. As 36 is a perfect square, a = b = 6 is one of the factors. For all other factor pairs (a, b), a < 6 and b > 6 holds. Summing up, we have the following:

Every number n can be written as n = a x bIf n is a perfect square, then a = b = √n.And if a < b, then, a < √n and b > √n.Else, if a > b, then a > √n and b < √n.

So instead of looping through all integers up to n/2, you may choose to run up to √n. And this is a lot more efficient than our previous approach.

How to Modify is_prime() to O(√n) Algorithm

Let’s proceed to optimize the function to check for prime numbers in Python. Now, let’s parse the above function definition:

To compute the square root of a number, let’s import Python’s built-in math module, and use math.sqrt() function. As n may not be a perfect square, we’ll have to cast it into an integer. Use int(var) to cast var into an int.To make sure we’re actually checking up to √n, we add a plus one as the range() function excludes the endpoint of the range by default.

The code cell below verifies that our function is_prime() works correctly. In the next section, let us create a few simple plots to understand O(n) and O(√n) visually.

Comparing O(n) and O(√n) Visually

▶️ Run the following code snippet in a Jupyter notebook environment of your choice. The above snippet shows how you can plot the curves for n and √n for a range of values.

We use the NumPy arange() function to create an array of numbers. And then, we collect the values of n and √n up to, but not including 20, into a pandas DataFrame.Next, we plot using seaborn’s lineplot() function.

From the plot below, we see that √n is significantly smaller than n. Let us now increase the range to as high as 2000 and repeat the same steps as above. From the above plot, you can infer that O(√n) algorithm is significantly faster when you’re testing if a large number is prime. Here’s a quick example: 2377 is a prime number—verify this!😀 While the O(n) approach will take the order of 2000 steps, the O(√n) algorithm can help arrive at the answer in just 49 steps.✅

Conclusion

⏳ And it’s time for a quick summary.

To check if a number is prime, the naïve approach is to loop through all numbers in the range (2, n-1). If you don’t find a factor that divides n, then n is prime.As the only factor of n greater than n/2 is n itself, you may choose to run only up to n/2.Both of the above approaches have a time complexity of O(n).As factors of a number occur in pairs, you can run only up to √n. This algorithm is a lot faster than O(n). And the gain is appreciable when checking if a large number is prime or not.

I hope you understand how to check if a number is prime in Python. As a next step, you may check out our tutorial on Python programs on string operations—where you’ll learn to check if strings are palindromes, anagrams, and more. See you all in another tutorial. Until then, happy coding!👩🏽‍💻

How to Check if a Number is Prime in Python - 78How to Check if a Number is Prime in Python - 78How to Check if a Number is Prime in Python - 83How to Check if a Number is Prime in Python - 19How to Check if a Number is Prime in Python - 66How to Check if a Number is Prime in Python - 49How to Check if a Number is Prime in Python - 23How to Check if a Number is Prime in Python - 30How to Check if a Number is Prime in Python - 52