Definition
n > 1
Composite means there exists d with 1 < d < n and n % d == 0.
A composite number is an integer greater than 1 with a nontrivial divisor. This tutorial covers the definition vs prime, a live preview, algorithm steps, worked Python examples, edge cases, and complexity.
n > 1
Composite means there exists d with 1 < d < n and n % d == 0.
Divisor count
Prime: exactly two divisors. Composite: more than two. 1: neither.
Trial division
Any factor pair has one factor ≤ √n — stop the loop there.
First hit
Return true as soon as any nontrivial divisor is found.
Try any n
Classify a number as composite, prime, or neither instantly.
Complexity
Optimized check uses O(√n) time and O(1) extra space.
Composite numbers are integers n > 1 that are not prime — they have at least one divisor strictly between 1 and n. Classic examples: 4, 6, 8, 9, 10, 12.
The number 1 is neither prime nor composite. Finding any nontrivial divisor is enough to prove compositeness; you do not need to list every factor.
It drills the prime/composite distinction, trial division, and the √n optimization interviewers expect.
One divisor in (1, n) proves composite.
Neither prime nor composite — always handle n ≤ 1.
Factor pairs guarantee a small factor ≤ √n.
4 = 2 × 2 is the first composite.
In short: if n > 1 and any i from 2…√n divides n, then n is composite; otherwise (for n > 1) it is prime.
Given an integer n, decide whether it is composite. Optionally list composites in a range.
# 12 → divisible by 2 → composite
# 7 → no divisor in 2..√7 → not composite (prime)
# 1 → neither | Item | Type | Description |
|---|---|---|
n | int | Integer to classify (composite defined for n > 1). |
| Return / print | bool / text | True if composite; otherwise not (prime or neither). |
function is_composite(n):
if n <= 1:
return false
i = 2
while i * i <= n:
if n % i == 0:
return true
i = i + 1
return false | Method | Idea | Notes |
|---|---|---|
| Trial to n/2 | Check every i up to n/2 | Simple but O(n) |
| Trial to √n | Loop while i * i ≤ n | Standard interview check |
| Show a factor | Return first divisor found | Great for explaining why |
| Goal | Pattern |
|---|---|
| Guard n ≤ 1 | if number <= 1: return False |
| √n loop | while i * i <= number: |
| Divisor hit | if number % i == 0: return True |
| Range filter | if is_composite(num): print(num) |
| Smallest composite | 4 |
| Neither case | 1 (and usually n ≤ 1) |
Three mutually exclusive buckets for positive integers.
2 divisorsOnly 1 and itself — e.g. 2, 3, 5, 7
> 2 divisorsHas a nontrivial factor — e.g. 4, 6, 9, 12
n = 1Unit — not prime, not composite
handle n<=1Say the definition before coding the loop
Reach for composite checks when classifying integers next to primes.
Tests definition accuracy and √n trial division.
Pairs naturally with the prime-number lesson.
Print all composites in 1…N for small N.
Once composite, the next question is often “find a factor.”
Standard definition applies to integers greater than 1.
Key benefit: one short boolean check that forces precise definitions and the classic √n optimization.
Enter an integer to check whether it is composite.
Three complete Python programs — single-number check, range listing, and a factor-proof helper. Click View Output to reveal sample console results.
Boolean check with √n trial division.
Short function, fast early return, and integer-safe loop bound.
def is_composite(number: int) -> bool:
if number <= 1:
return False
i = 2
while i * i <= number:
if number % i == 0:
return True
i += 1
return False
n = 12
if is_composite(n):
print(f"{n} is a composite number.")
else:
print(f"{n} is not a composite number.") Values ≤ 1 return False immediately. The loop stops at i * i <= number because any factor above √n has a matching factor below √n.
Reuse the helper to filter a small interval.
Print only values that satisfy the composite test.
def is_composite(number: int) -> bool:
if number <= 1:
return False
for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
return True
return False
print("Composite numbers in the range 1 to 10 are:")
for num in range(1, 11):
if is_composite(num):
print(num, end=" ") Same √n test, expressed with range(2, int(number ** 0.5) + 1). From 1 to 10 the composites are exactly 4, 6, 8, 9, 10.
Return the first nontrivial factor for explanations.
If composite, report one divisor that proves it.
def first_factor(number: int) -> int | None:
"""Return a nontrivial divisor, or None if not composite."""
if number <= 1:
return None
i = 2
while i * i <= number:
if number % i == 0:
return i
i += 1
return None
for n in (12, 7, 1, 9):
f = first_factor(n)
if f is None:
label = "neither" if n <= 1 else "prime"
print(f"{n}: not composite ({label})")
else:
print(f"{n}: composite (divisible by {f})") Same loop as is_composite, but returns the divisor instead of a boolean. Handy in interviews when the follow-up is “show me a factor.”
Not composite (neither prime nor composite).
Loop i from 2 while i * i ≤ n.
If n % i == 0, return true — proven composite.
No divisor found → not composite (for n > 1 that means prime).
n = 35Trace trial division up to √35 ≈ 5.9.
| i | i * i ≤ 35? | 35 % i | Action |
|---|---|---|---|
2 | Yes | 1 | Continue |
3 | Yes | 2 | Continue |
4 | Yes | 3 | Continue |
5 | Yes | 0 | Return composite |
Final: 35 is composite (divisible by 5; 35 = 5 × 7).
Where composite checks show up beyond the interview prompt.
Definition + √n loop in one tight problem.
Example: write is_composite(n).
Makes “more than two divisors” concrete.
Example: chalkboard 12 vs 7.
List composites in a classroom range.
Example: 1 to 10 → 4 6 8 9 10.
Once composite, find prime factors next.
Example: Smith-number pipelines.
Argue why √n beats scanning to n/2.
Example: “why stop at sqrt?”
Forces handling of 1, 2, and negatives.
Example: classify 1 correctly.
Pro Tip: say “composite = n > 1 and not prime” before coding — then implement the divisor search.
Why this pattern works well in interviews and classwork.
One nontrivial divisor is enough — no full factor list required.
Same bound used in prime checks — transferable skill.
A few integers suffice — O(1) extra space.
Even composites like 12 return after the first divisor.
Pro Tip: prefer while i * i <= n over float sqrt when interviewers care about integer precision.
Small habits that keep composite checks interview-ready.
State n > 1 with a nontrivial divisor, and that 1 is neither.
Explain why scanning past √n is unnecessary.
Do not keep looping after finding a divisor.
Assert 4, 9, 12 are composite and 2, 3, 7 are not.
Say the definition is for integers > 1 only.
Pro Tip: if asked for a proof, return the first factor — same loop, better storytelling.
Mistakes that commonly break composite-number solutions.
1 has only one positive divisor.
→ Return false / “neither” for n ≤ 1.
Both are prime.
→ The √n loop finds no divisor for them.
Wasteful and signals weak number sense.
→ Stop at √n (or i * i ≤ n).
Forgetting + 1 with int(sqrt(n)) can miss a factor.
→ Prefer while i * i <= n.
Standard school definition uses integers > 1.
→ Reject or document negatives explicitly.
Check these inputs before calling the solution done.
Do not mark 1 as prime or composite.
Both are not composite.
4 = 2 × 2 — first positive composite.
Composite classification is for integers greater than 1.
Still composite if > 1 (except 1 itself).
Use integer i * i <= n to avoid float drift.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
Quick Takeaway: for n > 1, any divisor in 2…√n proves composite; otherwise the number is prime.
| Method | Time | Extra space |
|---|---|---|
| Trial division to n/2 | O(n) | O(1) |
| Trial division to √n | O(√n) | O(1) |
| Range 1…N with √n check | O(N √N) | O(1) |
Composite numbers are integers greater than 1 with a nontrivial divisor. Guard n ≤ 1, scan up to √n, and return early on the first hit.
Practice the three examples above, then continue to Smith numbers for a composite-number follow-up that uses prime factors and digit sums.
Always handle 1 correctly, explain the √n bound, and state O(√n) time.
Classify integers the interview-friendly way.
n > 1 + factor
Definition1 is special
GuardScan to √n
MathFirst divisor
CodeO(√n) time
AnalysisThe number 1 is neither prime nor composite. The smallest composite number is 4.
Learn how some composites have digit sums equal to the digit sums of their prime factors.
9 people found this page helpful