This is a detailed tutorial to check if a number is prime or not in Python. Explained with the help of an illustrative example based on simple logic.
Table of Contents
Prime Numbers
These are the numbers that are greater than one and are not divisible by any of the numbers smaller than it. In other words, these are just divisible by themselves. The First 5 prime numbers are 2, 3, 5, 7 and 11. 4 is not a prime number because the number 2 is small that it and it is a factor of it. Similarly, 6 has 3 as its factor and 8 and 9 have 2, 4 and 3 as their factors respectively. Therefore, all the numbers which do not have smaller factors are called Prime Numbers. You can find out more detailed information on prime numbers here.
Checking Prime Numbers in Python
We can figure out if a number is prime or not easily in any of the different programming languages such as Python. In Python, we’ll create a function that will return True
if a number is prime and False
if a number is not prime.
But before we can actually create a function to check if a number is prime or not, we actually need to define a logic on the basis of which that function will return the boolean value. We need to make use of the following concepts of Python in our program.
Logic
Let’s create logic to check if a number is prime or not. A number if prime if and only if it satisfied the following conditions.
- It is greater than 1.
- It has no smaller factors.
So, we can form our logic as defined below.
- First of all, using an if-else statement to check if the given number is greater than one or not.
- Then, the modulus of the given number with all the possible small numbers will be checked. A For loop can be used to loop through all the small numbers starting from 2 upto the number, just before the given number. Again, we can use an if statement to check the modulus condition.
- An else statement is also used to return
True
in accordance with the for-loop. The else statement here will only be executed in case, divisible factors will be discovered.
I hope the logic is clear. You can also define your own logic as their might be several different ways to implement the same thing and hence the logic can also be varied slightly.
Example
The following python program contains a function that checks out if a given number is prime or not. This function is completely based on the logic that we’ve just discussed above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
def isPrime(number): if number > 1: #Number should be greater than 1 for x in range(2, number): #loop from 2 to number - 1 if (number % x) == 0: #In case any small number is divisible by the number return False else: #If No Small Numbers than the numbers are divisible by the number return True else: #As 1 is not a prime number #So return False return False #Checking different Numbers print(isPrime(1)) #False print(isPrime(2)) #True print(isPrime(3)) #True print(isPrime(4)) #False print(isPrime(5)) #True print(isPrime(5487)) #False #Another Use Case if isPrime(15): print("Given number is Prime.") else: print("Given number is not Prime.") |
Output.
Let’s understand how the isPrime(number)
function works. First of all, it checks if the argument number is greater than one or not. If it’s 1, then the function simply returns False
. If the number is greater than 1, a loop is being executed looping from 2 to the number, just one less than itself. For example, let’s say the given number is 10. Then it loops through 2 to 9. Every number from 2 to 9 is being checked for modulus division with 10. And in case if any of the modulus division results come out to be 0, then the function simply returns False
.
In case, none of the modulus division results out to be 0, only then the function returns True
. This is as per the logic as a number is prime if and only if it is not divisible by any of the integer numbers greater than one and smaller than itself. Hence, the program is justified.
I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!