Syntax error Java program to display prime numbers between intervals using function

Java program to display prime numbers between intervals using function



In this article, we will understand how to display prime numbers between intervals using a function in Java. We will be using two approaches: one with user input and the other with predefined input.

Prime numbers

The prime numbers are special numbers that have only two factors 1 and itself and cannot be divided by any other number.

A number is a prime number if its only factors are 1 and itself. 11 is a prime number. Its factors are 1 and 11 itself. Some examples of prime numbers are 2, 3, 5, 7, 11, 13, and so on. 2 is the only even prime number. All other prime numbers are odd numbers.

Problem Statement

Write a program in Java to display prime numbers between intervals using a function. Below is a demonstration of the same ?

Input

Starting number : 1
Ending number : 75

Output

The prime numbers between the interval 1 and 75 are:
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73

Approaches to display prime numbers between intervals

Following are the steps to display prime numbers between intervals using the function ?

Basic Iterative Check

This approach is straightforward and works by individually checking each number in the given range to determine if it is a prime. A prime number is defined as a number greater than 1 that is divisible only by 1 and itself.
Following are the steps to display prime numbers between intervals using the function ?

  • This function takes a number as input and checks divisibility for all integers from 2 to the square root of the number.
  • If any divisor is found, the function returns false, meaning the number is not prime.
  • Otherwise, it returns true.

Example

Following is an example of displaying prime numbers between intervals using an iterative approach ?

public class PrimeNumbers {
    public static void main(String[] args) {
        int start = 10, end = 50;
        System.out.println("Prime numbers between " + start + " and " + end + " are:");
        for (int i = start; i <= end; i++) {
            if (isPrime(i)) {
                System.out.print(i + " ");
            }
        }
    }

    // Function to check if a number is prime
    public static boolean isPrime(int num) {
        if (num <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
}

Output

Prime numbers between 10 and 50 are:
11 13 17 19 23 29 31 37 41 43 47

Time Complexity: O(m*sqrt{N}), as each number in the range is checked up to its square root.
Space Complexity: O(1), as it uses no extra data structures.

Using Sieve of Eratosthenes

The Sieve of Eratosthenes is an efficient algorithm for finding all prime numbers up to a given limit. Instead of testing each number in the range individually, this approach preprocesses prime numbers using a boolean array.

How it Works?

  • We create a boolean array isPrime where each index represents whether the corresponding number is prime.
  • All numbers are initially marked as prime (set to true), except for 0 and 1, which are not prime.
  • Starting from the smallest prime number (2), we iteratively mark all multiples of each prime as false (not prime).
  • After preprocessing, the array contains all prime numbers up to end.
  • Finally, we filter and print the prime numbers in the specified range [start, end].

Example

Following is an example of displaying prime numbers between intervals using the Sieve of Eratosthenes ?

import java.util.Arrays;

public class PrimeNumbers {
    public static void main(String[] args) {
        int start = 10, end = 50;
        System.out.println("Prime numbers between " + start + " and " + end + " are:");
        sievePrimes(start, end);
    }

    // Sieve of Eratosthenes to find all primes in a range
    public static void sievePrimes(int start, int end) {
        boolean[] isPrime = new boolean[end + 1];
        Arrays.fill(isPrime, true); // Assume all numbers are prime
        isPrime[0] = isPrime[1] = false; // 0 and 1 are not prime

        for (int i = 2; i * i <= end; i++) {
            if (isPrime[i]) {
                for (int j = i * i; j <= end; j += i) {
                    isPrime[j] = false; // Mark multiples as non-prime
                }
            }
        }

        // Print primes in the specified range
        for (int i = start; i <= end; i++) {
            if (isPrime[i]) {
                System.out.print(i + " ");
            }
        }
    }
}

Output

Prime numbers between 10 and 50 are:
11 13 17 19 23 29 31 37 41 43 47

Time Complexity: O(nlog?(log?(n+m))) where n = end, due to sieve preprocessing and range filtering.
Space Complexity: O(n), as it uses a boolean array to store primality up to the end.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2024-12-24T17:46:58+05:30

583 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements