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.

`// 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"` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**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)
- Java Program to Check if two Arrays are Equal or not
- Java Program to Check if a Directory is Empty or Not
- Java Program for Find largest prime factor of a number
- Java Program for efficiently print all prime factors of a given number
- Java Program to find Product of unique prime factors of a number
- Java Program For Closest Prime Number
- Check if a number is binary or not in Java
- Java Program for How to check if a given number is Fibonacci number?
- Check if Two Integers are Equal or Not in Java
- Check whether the number formed by concatenating two numbers is a perfect square or not
- Java Program to Check if all digits of a number divide it
- Java Program to check whether it is possible to make a divisible by 3 number using all digits in an array
- Java Program to check Armstrong Number
- Java Program to Reverse a Number & Check if it is a Palindrome
- Java Program to Check Whether Number is Divisible by 5
- Java Program to Find the Number Occurring Odd Number of Times
- Java Program to Convert a Decimal Number to Binary & Count the Number of 1s
- Java Program to Check if count of divisors is even or odd
- Java Program to check the validity of a Password using User Defined Exception
- Java Program to Check if a Given Integer is Positive or Negative

`// 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"` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

true false

Time complexity of this solution is O(√n)

Main Article : Primality Test | Set 1 (Introduction and School 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

## 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.