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 - falseCheck if 3 is a factor of 331 - falseCheck if 4 is a factor of 331 - falseCheck if 5 is a factor of 331 - falseCheck if 6 is a factor of 331 - falseCheck if 7 is a factor of 331 - falseCheck if 8 is a factor of 331 - falseCheck if 9 is a factor of 331 - falseCheck if 10 is a factor of 331 - falseCheck if 11 is a factor of 331 - falseCheck if 12 is a factor of 331 - falseCheck if 13 is a factor of 331 - falseCheck if 14 is a factor of 331 - falseCheck if 15 is a factor of 331 - falseCheck if 16 is a factor of 331 - falseCheck if 17 is a factor of 331 - falseCheck 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.
def is_prime(number):
if number <= 1:
return False
for i in range(2, int(number**0.5) + 1):
if (number % i == 0):
return False
return True
number_to_check = 331
if is_prime(number_to_check):
print(f"{number_to_check} is a prime number.")
else:
print(f"{number_to_check} is not a prime number.")
If you run this program, you will get:
331 is a prime number.
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:
import sympy as s
print(s.isprime(331))
The output is
True