Kodeclik Logo

Our Programs

Courses

Gifting

Learn More

Schedule

Kodeclik Blog

Is 331 a prime number?

Let us first recall what a prime number is. A prime number is a positive integer greater than 1 such that it has only two divisors (typically 1 and itself). For instance, the divisors of 2 are 1 and 2, and thus 2 is a prime number. The divisors of 3 are 1 and 3 and thus 3 is also a prime number. The divisors of 4 are 1, 2, and 4, and therefore it is not a prime number because it has three factors. In fact, one way to verify that 4 is not prime is to notice that 4 can be expressed as 2 times 2, whereas prime numbers can only be expressed as that number times itself.

Checking if 331 is a prime by systematic enumeration

Let us systematically enumerate factors from 2 and see if they divide our given number 331 evenly. We start at 2 and need to go only till the (approximate) square root of 331. (Why? Think about it.) Of course, if you know that 331 has an exact square root, you already know a factor (its square root!) so you can conclude that it is not a prime.

For instance, in our example, to find if 331 is a prime number we can aim to check for factors from 2 to 18.

Check if 2 is a factor of 331 - false
Check if 3 is a factor of 331 - false
Check if 4 is a factor of 331 - false
Check if 5 is a factor of 331 - false
Check if 6 is a factor of 331 - false
Check if 7 is a factor of 331 - false
Check if 8 is a factor of 331 - false
Check if 9 is a factor of 331 - false
Check if 10 is a factor of 331 - false
Check if 11 is a factor of 331 - false
Check if 12 is a factor of 331 - false
Check if 13 is a factor of 331 - false
Check if 14 is a factor of 331 - false
Check if 15 is a factor of 331 - false
Check if 16 is a factor of 331 - false
Check if 17 is a factor of 331 - false
Check if 18 is a factor of 331 - false

(We stop as soon as we find a factor because that indicates to us that the number is not a prime number. )

Also as described above, you typically do not need to go all the way to the given number to enumerate factors. We only need to check till the square root.

In our case, thus we learn that 331 is indeed a prime number.

Checking if 331 is a prime using Python

You can automate the above process by writing a Python program.

You can also take advantage of numerous Python libraries that provide in-built capabilities for primality testing.

Checking if 331 is a prime using Python SymPy

SymPy is a powerful symbolic mathematics library that includes various mathematical operations and functions, including primality testing. You can use the isprime() function from the SymPy library in Python to check if our given number 331 is prime. Here's how you can use it:

The output is

as expected. The isprime() function from SymPy returns True if the input number is prime and False if it's not. This function uses efficient algorithms for primality testing, making it a reliable choice when working with prime numbers in Python.

So in conclusion it is very easy to check if a number is prime using pencil and paper, using Python by coding your own function, or using a Python module.

If you liked this example, checkout the following additional problems that will help you get comfortable with the idea of primality checking.

Is 337 a prime?
Is 361 a prime?
Is 397 a prime?
Is 499 a prime?
Is 503 a prime?

Interested in learning Python? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.