Check if Number is Prime in DSA Java - Time & Space Complexity
We want to understand how the time to check if a number is prime grows as the number gets bigger.
How many steps does it take to decide if a number has no divisors other than 1 and itself?
Analyze the time complexity of the following code snippet.
public boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i < n; i++) {
if (n % i == 0) return false;
}
return true;
}
This code checks if a number n is prime by testing all numbers from 2 up to n-1 for divisibility.
- Primary operation: The for-loop that checks divisibility from 2 to n-1.
- How many times: Up to n-2 times in the worst case (when n is prime).
As the number n grows, the loop runs almost n times, so the steps increase roughly in a straight line with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 8 checks |
| 100 | Up to 98 checks |
| 1000 | Up to 998 checks |
Pattern observation: The number of steps grows linearly as n increases.
Time Complexity: O(n)
This means the time to check if a number is prime grows roughly in direct proportion to the size of the number.
[X] Wrong: "We only need to check divisibility up to n/2 or even less."
[OK] Correct: Checking up to n/2 is still linear and not optimal; the better limit is the square root of n, which reduces steps significantly.
Understanding how the number of checks grows helps you explain your solution clearly and shows you know how to analyze efficiency, a key skill in interviews.
"What if we only check divisibility up to the square root of n? How would the time complexity change?"
Practice
prime?Solution
Step 1: Understand the definition of prime number
A prime number is a number greater than 1 that has no divisors other than 1 and itself.Step 2: Compare options with definition
Only It has exactly two distinct positive divisors: 1 and itself. correctly states that prime numbers have exactly two distinct positive divisors: 1 and itself.Final Answer:
It has exactly two distinct positive divisors: 1 and itself. -> Option AQuick Check:
Prime number = two divisors [OK]
- Thinking even numbers are always prime
- Confusing prime with odd numbers
- Assuming 1 is prime
n is prime?Solution
Step 1: Analyze the loop range for checking divisors
We check divisors from 2 up to but not including n to see if n is divisible by any number other than 1 and itself.Step 2: Check each option's logic
for (int i = 2; i < n; i++) { if (n % i == 0) return false; } return true; correctly loops from 2 to n-1 and returns false if a divisor is found, else true. for (int i = 2; i <= n; i++) { if (n % i == 0) return false; } return true; incorrectly includes n itself, causing false negatives. for (int i = 1; i < n; i++) { if (n % i == 0) return false; } return true; starts from 1, which divides all numbers. for (int i = 2; i < n / 2; i++) { if (n % i == 0) return true; } return false; returns true on finding a divisor but should return false.Final Answer:
for (int i = 2; i < n; i++) { if (n % i == 0) return false; } return true; -> Option DQuick Check:
Loop 2 to n-1 and check divisibility [OK]
- Checking divisibility including n itself
- Starting loop from 1 instead of 2
- Returning true when divisor found
n = 11?
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
System.out.println(isPrime);Solution
Step 1: Understand the loop range and condition
The loop runs from 2 to the square root of 11 (approximately 3.3), so i takes values 2 and 3.Step 2: Check divisibility for i=2 and i=3
11 % 2 != 0 and 11 % 3 != 0, so no divisor found, isPrime remains true.Final Answer:
true -> Option AQuick Check:
11 is prime, so output is true [OK]
- Looping up to n instead of sqrt(n)
- Forgetting to break loop on divisor found
- Misunderstanding boolean flag usage
n is prime:
boolean isPrime = true;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
isPrime = true;
break;
}
}
System.out.println(isPrime);Solution
Step 1: Analyze the flag update inside the loop
When a divisor is found, isPrime should be set to false, but here it is incorrectly set to true.Step 2: Check loop range and starting point
The loop from 2 to n/2 is acceptable for checking divisors, so no error there.Final Answer:
The flag isPrime should be set to false when divisor found, not true. -> Option BQuick Check:
Set isPrime = false on divisor found [OK]
- Setting isPrime true inside divisor check
- Looping from 1 instead of 2
- Ignoring break after finding divisor
isPrime(int n) that returns true if n is prime and false otherwise. Which of the following improvements makes the method more efficient for large n?Solution
Step 1: Understand the divisor checking range
Any divisor larger than the square root of n would have a corresponding divisor smaller than the square root, so checking beyond sqrt(n) is unnecessary.Step 2: Evaluate options for efficiency
Check divisibility only up to the square root ofninstead ofn-1. reduces the number of checks drastically, improving efficiency. Check divisibility by all numbers from 2 ton-1without any break. is inefficient. Check divisibility only by even numbers. is wrong because even numbers except 2 are not prime. Check divisibility by numbers greater thann/2only. misses smaller divisors.Final Answer:
Check divisibility only up to the square root of n instead of n-1. -> Option CQuick Check:
Check divisors up to sqrt(n) for efficiency [OK]
- Checking all numbers up to n-1
- Checking only even numbers
- Ignoring divisor pairs below sqrt(n)
Ready to prove it?
Pass the test and this pattern moves unknown
