Check if a number is prime number in Java

What is a prime number?

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, a prime number is a whole number that cannot be evenly divided by any other whole number except for 1 and itself.

Here are a few examples of prime numbers:

  • 2 is the smallest prime number because it can only be divided by 1 and 2.
  • 3 is also a prime number because it can only be divided by 1 and 3.
  • 5 is a prime number because it can only be divided by 1 and 5.

Program to find if a number is prime or not

To determine if a number is prime in Java, we can write a simple program that checks whether the number is divisible by any integer between 2 and the square root of the number. If it’s not divisible by any of these integers, then it’s a prime number. Here’s a Java program to do this:

				
					import java.util.Scanner;

public class PrimeNumberChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        
        int number = scanner.nextInt();
        scanner.close();
        
        if (isPrime(number)) {
            System.out.println(number + " is a prime number.");
        } else {
            System.out.println(number + " is not a prime number.");
        }
    }
    
    public static boolean isPrime(int number) {
        if (number <= 1) {
            return false; // 0 and 1 are not prime numbers
        }
        
        if (number <= 3) {
            return true; // 2 and 3 are prime numbers
        }
        
        if (number % 2 == 0 || number % 3 == 0) {
            return false; // Numbers divisible by 2 or 3 are not prime
        }
        
        // Check for prime numbers using 6k +/- 1 rule
        for (int i = 5; i * i <= number; i += 6) {
            if (number % i == 0 || number % (i + 2) == 0) {
                return false;
            }
        }
        
        return true;
    }
}

				
			

Here’s a explanation of above code:

main Method:
  • The main method is the entry point of the program. It starts by creating a Scanner object to read user input from the console.
  • It prompts the user to “Enter a number:” and reads the input as an integer.
  • The scanner.close() method is called to close the Scanner object, freeing up resources.
  • The isPrime method is then called with the input number as an argument to check if it’s prime.
    • If isPrime returns true, it prints that the number “is a prime number.”
    • Otherwise, it prints that the number “is not a prime number.”
isPrime Method:

The isPrime method determines whether a given integer is prime using several checks:

  • Step 1: Checks if number is less than or equal to 1. Numbers 0 and 1 are not prime, so it returns false.
  • Step 2: Checks if number is less than or equal to 3. Numbers 2 and 3 are prime, so it returns true.
  • Step 3: Checks if number is divisible by 2 or 3. If it is, then it’s not a prime number, so it returns false.
  • Step 4: Uses a loop to check for other factors up to the square root of the number:
    • It uses the optimization 6k ± 1, which skips multiples of 2 and 3 by incrementing i by 6 each time. This is because all prime numbers greater than 3 can be represented as 6k ± 1.
    • For each i, it checks if number is divisible by i or i + 2. If so, it returns false because it means the number has a factor other than 1 and itself.
  • If none of these checks return false, it means the number is prime, and it returns true.

Overall, this method is efficient for prime number checking, using optimizations to reduce the number of divisions needed.