Java Program to Find Prime Numbers in a Range
start to end and check each number with a method that tests divisibility by numbers from 2 to its square root to find prime numbers.Examples
How to Think About It
Algorithm
Code
public class PrimeRange { public static boolean isPrime(int num) { if (num <= 1) return false; for (int i = 2; i * i <= num; i++) { if (num % i == 0) return false; } return true; } public static void main(String[] args) { int start = 1, end = 20; for (int i = start; i <= end; i++) { if (isPrime(i)) { System.out.print(i + " "); } } } }
Dry Run
Let's trace the program for the range 1 to 10.
Check number 1
1 is not prime because it is <= 1.
Check number 2
2 is prime because no divisor found between 2 and sqrt(2).
Check number 3
3 is prime because no divisor found between 2 and sqrt(3).
Check number 4
4 is not prime because 4 % 2 == 0.
Check number 5
5 is prime because no divisor found between 2 and sqrt(5).
Check number 6
6 is not prime because 6 % 2 == 0.
Check number 7
7 is prime because no divisor found between 2 and sqrt(7).
Check number 8
8 is not prime because 8 % 2 == 0.
Check number 9
9 is not prime because 9 % 3 == 0.
Check number 10
10 is not prime because 10 % 2 == 0.
| Number | Is Prime? |
|---|---|
| 1 | No |
| 2 | Yes |
| 3 | Yes |
| 4 | No |
| 5 | Yes |
| 6 | No |
| 7 | Yes |
| 8 | No |
| 9 | No |
| 10 | No |
Why This Works
Step 1: Check numbers in range
The program loops through each number from start to end to test if it is prime.
Step 2: Prime check logic
For each number, it checks divisibility from 2 up to the square root of the number using num % i == 0. If divisible, it is not prime.
Step 3: Print prime numbers
Numbers that pass the test are printed as prime numbers in the range.
Alternative Approaches
public class PrimeRangeSieve { public static void main(String[] args) { int start = 1, end = 20; boolean[] prime = new boolean[end + 1]; for (int i = 2; i <= end; i++) prime[i] = true; for (int p = 2; p * p <= end; p++) { if (prime[p]) { for (int i = p * p; i <= end; i += p) { prime[i] = false; } } } for (int i = start; i <= end; i++) { if (prime[i]) System.out.print(i + " "); } } }
public class PrimeRangeHalfCheck { public static boolean isPrime(int num) { if (num <= 1) return false; for (int i = 2; i <= num / 2; i++) { if (num % i == 0) return false; } return true; } public static void main(String[] args) { int start = 1, end = 20; for (int i = start; i <= end; i++) { if (isPrime(i)) System.out.print(i + " "); } } }
Complexity: O(n * sqrt(m)) time, O(1) space
Time Complexity
The program checks each number in the range (n numbers) and for each number checks divisibility up to its square root (sqrt(m)), so total time is O(n * sqrt(m)).
Space Complexity
The program uses constant extra space for variables and no additional data structures, so space complexity is O(1).
Which Approach is Fastest?
The Sieve of Eratosthenes is faster for large ranges but uses extra memory, while the simple divisibility check is easier to understand and uses less memory.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Simple divisibility up to sqrt(n) | O(n * sqrt(m)) | O(1) | Small to medium ranges, easy to implement |
| Sieve of Eratosthenes | O(n log log n) | O(n) | Large ranges, faster performance |
| Divisibility up to n/2 | O(n * n) | O(1) | Educational, but inefficient |