Given a positive integer, check if the number is prime or not. A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples of first few prime numbers are {2, 3, 5,
Examples :
Input: n = 11 Output: true Input: n = 15 Output: false Input: n = 1 Output: false
School Method
A simple solution is to iterate through all numbers from 2 to n-1 and for every number check if it divides n. If we find any number that divides, we return false.
Below is the implementation of this method.
C++
// A school method based C++ program to check if a // number is prime #include <bits/stdc++.h> using namespace std; bool isPrime(int n) { // Corner case if (n <= 1) return false; // Check from 2 to n-1 for (int i=2; i<n; i++) if (n%i == 0) return false; return true; } // Driver Program to test above function int main() { isPrime(11)? cout << " true\n": cout << " false\n"; isPrime(15)? cout << " true\n": cout << " false\n"; return 0; } |
Java
// A school method based JAVA program // to check if a number is prime class GFG { static boolean isPrime(int n) { // Corner case if (n <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < n; i++) if (n % i == 0) return false; return true; } // Driver Program public static void main(String args[]) { if(isPrime(11)) System.out.println(" true"); else System.out.println(" false"); if(isPrime(15)) System.out.println(" true"); else System.out.println(" false"); } } // This code is contributed // by Nikita Tiwari. |
Python3
# A school method based Python3 # program to check if a number # is prime def isPrime(n): # Corner case if n <= 1: return False # Check from 2 to n-1 for i in range(2, n): if n % i == 0: return False; return True # Driver Program to test above function print("true") if isPrime(11) else print("false") print("true") if isPrime(14) else print("false") # This code is contributed by Smitha Dinesh Semwal |
C#
// A optimized school method based C# // program to check if a number is prime using System; namespace prime { public class GFG { public static bool isprime(int n) { // Corner cases if (n <= 1) return false; for (int i = 2; i < n; i++) if (n % i == 0) return false; return true; } // Driver program public static void Main() { if (isprime(11)) Console.WriteLine("true"); else Console.WriteLine("false"); if (isprime(15)) Console.WriteLine("true"); else Console.WriteLine("false"); } } } // This code is contributed by Sam007 |
PHP
<?php // A school method based PHP // program to check if a number // is prime function isPrime($n) { // Corner case if ($n <= 1) return false; // Check from 2 to n-1 for ($i = 2; $i < $n; $i++) if ($n % $i == 0) return false; return true; } // Driver Code $tet = isPrime(11) ? " true\n" : " false\n"; echo $tet; $tet = isPrime(15) ? " true\n" : " false\n"; echo $tet; // This code is contributed by m_kit ?> |
Output :
true false
Time complexity of this solution is O(n)
Optimized School 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)
Below is the implementation of this solution.
C++
// A optimized school method based C++ program to check // if a number is prime #include <bits/stdc++.h> using namespace std; bool isPrime(int 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 || n%3 == 0) return false; for (int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } // Driver Program to test above function int main() { isPrime(11)? cout << " true\n": cout << " false\n"; isPrime(15)? cout << " true\n": cout << " false\n"; return 0; } |
Java
// A optimized school method based Java // program to check if a number is prime import java.io.*; class GFG { static boolean isPrime(int 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 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Driver Program public static void main(String args[]) { if(isPrime(11)) System.out.println(" true"); else System.out.println(" false"); if(isPrime(15)) System.out.println(" true"); else System.out.println(" false"); } } /*This code is contributed by Nikita Tiwari.*/ |
Python3
# 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. |
C#
// A optimized school method based C# // program to check if a number is prime using System; class GFG { public static bool isPrime(int 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 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Driver Code public static void Main() { if (isPrime(11)) Console.WriteLine("true"); else Console.WriteLine("false"); if (isPrime(15)) Console.WriteLine("true"); else Console.WriteLine("false"); } } // This code is contributed by aj_36 |
PHP
<?php // A optimized school method // based PHP program to check // if a number is prime function 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 || $n % 3 == 0) return false; for($i = 5; $i * $i <= $n; $i = $i + 6) if ($n % $i == 0 || $n % ($i + 2) == 0) return false; return true; } // Driver Code if(isPrime(11)) echo " true\n"; else echo " false\n"; if(isPrime(15)) echo " true\n"; else echo " false\n"; // This code is contributed ajit ?> |
Output :
true false
Time complexity of this solution is O(√n)
Primality Test | Set 2 (Fermat Method)
References:
https://en.wikipedia.org/wiki/Prime_number
http://www.cse.iitk.ac.in/users/manindra/presentations/FLTBasedTests.pdf
https://en.wikipedia.org/wiki/Primality_test
This article is contributed by Ajay. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

