Given a positive integer N. The task is to write a Python program to check if the number is prime or not.

**Definition: **A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11, ….}.

Examples :

Input: n = 11 Output: true Input: n = 15 Output: false Input: n = 1 Output: false

The idea to solve this problem is to iterate through all the numbers starting from 2 to (N/2) using a for loop and for every number check if it divides N. If we find any number that divides, we return false. If we did not find any number between 2 and N/2 which divides N then it means that N is prime and we will return True.

Below is the Python program to check if a number is prime:

`# Python program to check if ` `# given number is prime or not ` ` ` `num ` `=` `11` ` ` `# If given number is greater than 1 ` `if` `num > ` `1` `: ` ` ` ` ` `# Iterate from 2 to n / 2 ` ` ` `for` `i ` `in` `range` `(` `2` `, num): ` ` ` ` ` `# If num is divisible by any number between ` ` ` `# 2 and n / 2, it is not prime ` ` ` `if` `(num ` `%` `i) ` `=` `=` `0` `: ` ` ` `print` `(num, ` `"is not a prime number"` `) ` ` ` `break` ` ` `else` `: ` ` ` `print` `(num, ` `"is a prime number"` `) ` ` ` `else` `: ` ` ` `print` `(num, ` `"is not a prime number"` `) ` |

*chevron_right*

*filter_none*

**Output:**

11 is a prime number

** Optimized Method **

We can do following optimizations:

- Instead of checking till n, we can check till √n because a larger factor of n must be a multiple of smaller factor that has been already checked.
- The algorithm can be improved further by observing that all primes are of the form 6k ± 1, with the exception of 2 and 3. This is because all integers can be expressed as (6k + i) for some integer k and for i = ?1, 0, 1, 2, 3, or 4; 2 divides (6k + 0), (6k + 2), (6k + 4); and 3 divides (6k + 3). So a more efficient method is to test if n is divisible by 2 or 3, then to check through all the numbers of form 6k ± 1. (Source: wikipedia)
- Check whether the sum of prime elements of the array is prime or not
- C Program to Check Whether a Number is Prime or not
- Check whether a number is circular prime or not
- Check whether the given number is Wagstaff prime or not
- Check whether N is a Dihedral Prime Number or not
- Check whether a number is Good prime or not
- Check whether the sum of absolute difference of adjacent digits is Prime or not
- Check whether the given numbers are Cousin prime or not
- Check whether count of distinct characters in a string is Prime or not
- Check whether the frequencies of all the characters in a string are prime or not
- Program to check whether the given number is Buzz Number or not
- C Program to check whether a number is a Perfect Cube or not
- Check Whether a number is Duck Number or not
- Check whether the given number is Euclid Number or not
- Check whether given number N is a Moran Number or not
- Check whether a given number N is a Nude Number or not
- Check whether a given number is an ugly number or not
- Python regex | Check whether the input is Floating point number or not
- Check whether a number has exactly three distinct factors or not
- Check whether a number is semiprime or not

`# A optimized school method based ` `# Python3 program to check ` `# if a number is prime ` ` ` ` ` `def` `isPrime(n) : ` ` ` ` ` `# Corner cases ` ` ` `if` `(n <` `=` `1` `) : ` ` ` `return` `False` ` ` `if` `(n <` `=` `3` `) : ` ` ` `return` `True` ` ` ` ` `# This is checked so that we can skip ` ` ` `# middle five numbers in below loop ` ` ` `if` `(n ` `%` `2` `=` `=` `0` `or` `n ` `%` `3` `=` `=` `0` `) : ` ` ` `return` `False` ` ` ` ` `i ` `=` `5` ` ` `while` `(i ` `*` `i <` `=` `n) : ` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `or` `n ` `%` `(i ` `+` `2` `) ` `=` `=` `0` `) : ` ` ` `return` `False` ` ` `i ` `=` `i ` `+` `6` ` ` ` ` `return` `True` ` ` ` ` `# Driver Program ` `if` `(isPrime(` `11` `)) : ` ` ` `print` `(` `" true"` `) ` `else` `: ` ` ` `print` `(` `" false"` `) ` ` ` `if` `(isPrime(` `15` `)) : ` ` ` `print` `(` `" true"` `) ` `else` `: ` ` ` `print` `(` `" false"` `) ` ` ` ` ` `# This code is contributed ` `# by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

Main Article : Primality Test | Set 1 (Introduction and School Method)

## Recommended Posts:

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.