Definition
n > 1
Only divisors are 1 and n.
A prime number is an integer greater than 1 whose only positive divisors are 1 and itself. Examples: 2, 3, 5, 7, 11. Non-examples: 1, 4, 9, 15. This tutorial covers trial division up to sqrt(n), a live checker, worked Python examples, edge cases, and complexity.
n > 1
Only divisors are 1 and n.
Not prime
1, 0, negatives fail first.
i*i <= n
Stop at sqrt(n) safely.
2
All larger evens are composite.
Try 17 / 18
See verdict and reason.
n > 1
Composite = not prime (and > 1).
A prime number is an integer greater than 1 that has no positive divisors other than 1 and itself. So 17 is prime, while 15 = 3 × 5 is not.
Interviews almost always want trial division: reject n < 2, then test candidates while i * i <= n. One found factor proves composite; surviving the loop proves prime.
It is the most common number-theory interview check and the foundation for sieves, factorization, and cryptography warm-ups.
1 is not prime.
i*i <= n is enough.
Only even prime.
Interview complexity.
In short: guard n < 2, then trial divide while i * i <= n.
Given an integer n, return whether it is prime.
# 17 -> prime (no divisor up to sqrt(17))
# 15 -> not (3 divides 15)
# 2 -> prime (smallest / only even)
# 1 -> not (primes start at 2) | Item | Type | Description |
|---|---|---|
n | int | Value to classify. |
| Return | bool | True when n is prime. |
| Key bound | loop | while i * i <= n |
function isPrime(n):
if n < 2:
return false
for i from 2 while i * i <= n:
if n mod i = 0:
return false
return true | Method | Idea | Notes |
|---|---|---|
| Trial to sqrt | Test i while i*i <= n | Interview default |
| Skip evens | Handle 2, then odd i only | Faster constant factors |
| Sieve | Mark composites up to N | Best for many primes |
| Goal | Pattern |
|---|---|
| Reject small | if n <= 1: return False |
| Sqrt loop | while i * i <= n: |
| Composite hit | if n % i == 0: return False |
| Prime | return True |
| Skip evens | if n % 2 == 0: …; i += 2 |
| Interview line | Guard n < 2, trial to sqrt(n), O(sqrt(n)) |
Same question — pick the right tool.
i*i <= nClearest single-check answer
i += 2Same idea, fewer loops
mark up to NMany primes, one pass
stop earlyPrime check exits on first hit
Reach for a primality check whenever you need a true/false for “is n prime?”
Nearly every number-theory warm-up.
Print primes inside [a, b].
Same divisor thinking as factorization.
Why i*i <= n is enough.
Use a sieve when listing many primes.
Key benefit: one short helper that teaches guards, sqrt reasoning, and O(sqrt(n)) analysis in one shot.
Follows the same algorithm as the Python function: handle small values, then test divisors while i * i <= n.
Three complete Python programs — check 17, list primes from 1 to 20, and a faster odd-divisor variant. Click View Output to reveal sample console results.
The classic interview helper with a sqrt bound.
Handles n <= 1, then checks divisors only up to sqrt(n).
def is_prime(n: int) -> bool:
if n <= 1:
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
test_number = 17
if is_prime(test_number):
print(f"{test_number} is a prime number.")
else:
print(f"{test_number} is not a prime number.") For 17, candidates run while i * i <= 17 (i = 2, 3, 4). None divides 17, so the function returns True.
Reuse the helper across a small interval.
Reuse is_prime() and print primes in a small interval.
def is_prime(num: int) -> bool:
if num <= 1:
return False
i = 2
while i * i <= num:
if num % i == 0:
return False
i += 1
return True
start, end = 1, 20
print(f"Prime numbers in the range {start} to {end}:")
for value in range(start, end + 1):
if is_prime(value):
print(value, end=" ")
print() Each value from 1 to 20 is classified independently. 1 is rejected by the guard; composites fail on their first divisor.
Handle 2 specially, then test only odd candidates.
def is_prime(n: int) -> bool:
if n <= 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
i = 3
while i * i <= n:
if n % i == 0:
return False
i += 2
return True
for value in (2, 17, 18, 1, 19):
label = "prime" if is_prime(value) else "not prime"
print(f"{value}: {label}") After rejecting even n > 2, the loop only needs odd i. That roughly halves the number of modulo checks.
If n <= 1, it is not prime.
Loop i from 2 while i * i <= n.
If n % i == 0, return False immediately.
No divisor found up to sqrt(n).
Why the sqrt bound catches composites early and lets primes through.
| n | i checked (i*i <= n) | Hit? | Verdict |
|---|---|---|---|
17 | 2, 3, 4 | No | Prime |
15 | 2, then 3 | Yes (3) | Not prime |
2 | (loop does not run) | — | Prime |
1 | guard | — | Not prime |
If n = a * b with a, b > 1, at least one factor is <= sqrt(n) — so the loop is enough.
Where prime checks show up beyond the interview prompt.
Guards + sqrt trial division.
Example: is_prime(17).
List primes in a band.
Example: 1..20 list.
Same divisor mindset as factors.
Example: previous page.
Clarify neither/prime edge cases.
Example: live preview.
Mention when asked about speed.
Example: Example 3.
Continue the interview chain.
Example: related CTA.
Pro Tip: open with “Guard n < 2, then trial divide up to sqrt(n), so time is O(sqrt(n)).”
Why trial division works well for beginners and interviews.
Dry-run 15 and 17 on paper quickly.
O(sqrt(n)) is easy to justify.
Composites often fail on the first factor.
Skip evens or mention sieves later.
Pro Tip: lead with the simple sqrt loop; offer odd-only or sieve only if asked about speed or bulk queries.
Small habits that keep prime checks interview-ready.
Never skip the small-value check.
Avoid float sqrt if you can.
Only even prime — interviewers like hearing it.
Exit as soon as a divisor appears.
Do not trial-divide every n up to huge N.
Pro Tip: sanity-check 1, 2, 17, and 15 — if those four behave, your logic is solid.
Mistakes that commonly break prime-number programs.
1 has only one positive divisor.
→ Return False for n <= 1.
Unnecessary and slow.
→ Stop at i * i <= n.
Rounding can mis-bound the loop.
→ Prefer i * i <= n.
Primality is for integers > 1.
→ Treat negatives as not prime.
Checking every n to millions one-by-one.
→ Use a sieve for bulk listing.
Handle these before claiming the check is complete.
Neither prime nor composite.
Smallest and only even prime.
Divisible by 2.
Definition starts above 1.
No divisor up to sqrt(17).
Caught by factor 3.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
Quick Takeaway: guard n < 2, then trial divide while i*i <= n.
| Approach | Time | Extra space |
|---|---|---|
| Trial to sqrt(n) | O(sqrt(n)) | O(1) |
| Range check [a, b] | About O((b-a)*sqrt(b)) | O(1) |
| Sieve up to N | O(N log log N) | O(N) |
For interview demos, quote O(sqrt(n)) for a single check and mention sieves for bulk listing.
A prime is an integer greater than 1 with no divisor other than 1 and itself. Guard small values, trial divide up to sqrt(n), and remember that 2 is the only even prime.
Practice the three examples above, then continue to pronic numbers.
Guard n < 2, then trial divide while i*i <= n — that is the interview answer.
Classify primality the interview-friendly way.
n > 1 only
Definitioni*i <= n
Looponly even
Edge1 fails
GuardO(√n)
AnalysisThe only even prime is 2. Every integer greater than 1 is either prime or composite—except 1, which is neither. There are infinitely many primes.
Learn how to check whether a number is pronic in Python.
9 people found this page helpful