Java 8 Program To Find Prime Number

1. Introduction

Finding whether a number is prime—an integer greater than 1 that has no divisors other than 1 and itself—is a common task in both mathematics and computer science. It's a classic example used to demonstrate loops, conditionals, and efficiency in algorithms. With Java 8, new approaches to this problem have emerged, leveraging the Stream API for a more functional style of programming. This blog post will explore a Java 8 program designed to determine if a number is prime by using streams to simplify the process.

Java 8 Program To Find Prime Number

2. Program Steps

1. Define a method to check primality using a stream of integers.

2. In the method, generate a stream of numbers starting from 2 to the square root of the given number.

3. Use the stream to check if any number is a divisor of the given number.

4. Return true if no divisors are found; otherwise, return false.

5. Test the method with a specific number and print the result.

3. Code Program

import java.util.stream.IntStream;

public class PrimeNumberCheck {
    public static void main(String[] args) {
        int number = 29; // The number to check
        boolean isPrime = isPrime(number); // Check if the number is prime

        // Step 5: Print the result
        if (isPrime) {
            System.out.println(number + " is a prime number.");
        } else {
            System.out.println(number + " is not a prime number.");
        }
    }

    // Step 1: Define the method to check for a prime number
    public static boolean isPrime(int number) {
        // Edge case for number 1
        if (number <= 1) {
            return false;
        }
        // Step 2 & 3: Generate a stream and check for divisors
        return !IntStream.rangeClosed(2, (int) Math.sqrt(number))
                         .anyMatch(i -> number % i == 0); // Step 4: Return true if no divisors found
    }
}

Output:

29 is a prime number.

Explanation:

1. The program begins by defining a number to check for primality. In this example, the number is 29.

2. It calls the isPrime method, passing the number as an argument. This method is responsible for determining if the number is prime.

3. The isPrime method first handles an edge case: numbers less than or equal to 1 are not considered prime.

4. It then creates a stream of integers from 2 to the square root of the given number using IntStream.rangeClosed(). This range includes all possible factors of the number, except 1 and the number itself.

5. The method uses the anyMatch operation to check if any integer in the stream is a divisor of the given number (number % i == 0). If any divisors are found, anyMatch returns true, and the method returns false, indicating the number is not prime.

6. If no divisors are found, the method returns true, indicating the number is prime.

7. Finally, the result is printed. The output confirms that 29 is a prime number, demonstrating a concise and efficient way to solve this problem using Java 8's Stream API.

Comments