Check Whether a Number Is Prime
Determine whether an integer greater than 1 has no divisors other than 1 and itself.
- Time
O(sqrt(n)) - Space
O(1) - Hints3
- Read2 min
A number is prime if it is greater than 1 and has no divisor other than 1 and itself. Test candidate divisors from 2 up to the square root of n; if any divides n evenly, it is not prime. Checking only up to the square root is enough because factors come in pairs.
Problem statement
A prime number is an integer greater than 1 whose only positive divisors are
1 and itself. 2, 3, 5, 7, 11 are prime, while 4, 6, 9, 15 are not.
Given an integer n, return true if it is prime and false otherwise. For
efficiency, test possible divisors only up to the square root of n, since any
factor larger than the square root pairs with one smaller than it.
A single integer n.
true if n is prime, otherwise false.
Examples
29true29 has no divisor between 2 and 5 (its square root is about 5.39), so it is prime.
15false15 is divisible by 3, so it has a divisor other than 1 and itself and is not prime.
Constraints
- n can be any 32-bit int; numbers less than 2 are not prime
- Only test divisors up to sqrt(n) for efficiency
Think before you code
Reveal the questions to ask yourself first
- Why are 0, 1, and negative numbers not prime?
- Why is it enough to test divisors only up to the square root of n?
- What is the very first divisor you should test?
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
Approach
Reveal the step-by-step approach
Rule out small values, then trial-divide up to the square root.
- If
n < 2, returnfalse. - Loop
ifrom 2 whilei * i <= n:- If
n % i == 0,nhas a divisor other than 1 and itself, so returnfalse.
- If
- If no divisor is found, return
true.
Cast i * i to long inside the loop condition so the multiplication cannot
overflow for large n.
Dry run
Walk through the example step by step
Checking n = 29:
i | i*i | i*i <= 29 | 29 % i
--+-----+-----------+-------
2 | 4 | yes | 1
3 | 9 | yes | 2
4 | 16 | yes | 1
5 | 25 | yes | 4
6 | 36 | no -> stop | —
No divisor found, so 29 is prime -> true.
Solution
Reveal the full Java solution
public class PrimeCheck {
public static boolean isPrime(int n) {
if (n < 2) {
return false;
}
for (int i = 2; (long) i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(isPrime(29)); // true
System.out.println(isPrime(15)); // false
}
}Every composite number n has a factor no larger than sqrt(n), because if both
factors were bigger than the square root their product would exceed n. That is
why looping while i * i <= n is enough and turns an O(n) scan into O(sqrt n).
O(sqrt(n))O(1)Review before the interview
Common mistakes
- Treating 1 (or 0 and negatives) as prime; primes must be greater than 1.
- Looping i all the way to n instead of stopping at the square root, which is far slower.
Edge cases to test
- 2 is the smallest and only even prime; the loop body never runs, so it returns true.
- Perfect squares like 25 are caught when i reaches 5 (5 * 5 == 25).
Interview follow-ups
- How would you generate all primes up to n efficiently with the Sieve of Eratosthenes?
- How would you skip even divisors after checking 2 to roughly halve the work?
Keep practising
Print All Prime Numbers Between 1 and N
Hints · Dry run · SolutionSolve Similar problemCheck Whether a Number Is a Perfect Number
Hints · Dry run · SolutionSolve Related learningLoops
TutorialLearn Related learningOperators
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartPractising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
