Saturday, 22 July 2023

Segmented sieve Algorithm

import java.util.*;

public class Main {

    public static void main(String args[]) {


        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the Lower Bound : ");

        int lower_bound = sc.nextInt();

        System.out.println("Enter the Upper Bound : ");

        int upper_bound = sc.nextInt();


        boolean[] array = new boolean[upper_bound + 1];

        Arrays.fill(array, true); // Initialize all elements to true


        for (int i = 2; i * i <= upper_bound; i++) {

            if (array[i]) {

                for (int j = i * i; j <= upper_bound; j += i) {

                    array[j] = false;

                }

            }

        }


        System.out.println("Prime numbers between " + lower_bound + " and " + upper_bound + " are : ");

        for (int i = Math.max(2, lower_bound); i <= upper_bound; i++) {

            if (array[i]) {

                System.out.print(i + " ");

            }

        }

    }

}


EXPLANATION

1. The code starts by importing the required classes from the `java.util` package, which includes the `Scanner` class and the `Arrays` class.

2. In the `main` method, it creates a new `Scanner` object named `sc` to read input from the user.

3. The code then prompts the user to enter the lower bound and upper bound for finding prime numbers and stores the input in `lower_bound` and `upper_bound` variables, respectively.

4. It creates a boolean array named `array` with a size of `upper_bound + 1`, where all elements are initialized to `true`. This array will be used to track whether a number is prime or not.

5. The code then starts the Sieve of Eratosthenes algorithm. It iterates over `i` from 2 to the square root of the `upper_bound`.

6. Inside the outer loop, there's an if-condition to check if `array[i]` is `true`. If it is, it means `i` is a prime number, and we proceed to mark its multiples as non-prime in the `array`. The inner loop starts from `i * i`, as all multiples of `i` below `i * i` would have already been marked as non-prime by previous iterations. It continues marking multiples of `i` up to `upper_bound` as non-prime by setting their corresponding entries in the `array` to `false`.

7. After completing the Sieve of Eratosthenes algorithm, the code moves to the next part, which is displaying the prime numbers found between the specified `lower_bound` and `upper_bound`.

8. It prints the message "Prime numbers between `lower_bound` and `upper_bound` are:", and then it starts a loop to iterate from `Math.max(2, lower_bound)` to `upper_bound`.

9. In this loop, it checks if `array[i]` is `true`. If it is, then `i` is a prime number within the specified range, and it prints `i` followed by a space.

Let's illustrate the code's functionality with an example:

Suppose the user enters `lower_bound = 10` and `upper_bound = 30`. The code would then find and display the prime numbers between 10 and 30, which are: `11 13 17 19 23 29`.

Keep in mind that this code uses the Sieve of Eratosthenes algorithm, which is an efficient way to find prime numbers within a given range. It's particularly useful when you need to find multiple prime numbers in a large range.

No comments:

Post a Comment