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 JavaScript 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 | number | Integer to classify (composite defined for n > 1). |
| Return / print | boolean / text | true if composite; otherwise not (prime or neither). |
function isComposite(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 (isComposite(num)) console.log(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 JavaScript programs with Try it Yourself editors — 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.
function isComposite(number) {
if (number <= 1) {
return false;
}
let i = 2;
while (i * i <= number) {
if (number % i === 0) {
return true;
}
i += 1;
}
return false;
}
const n = 12;
if (isComposite(n)) {
console.log(n + " is a composite number.");
} else {
console.log(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.
function isComposite(number) {
if (number <= 1) {
return false;
}
for (let i = 2; i * i <= number; i++) {
if (number % i === 0) {
return true;
}
}
return false;
}
console.log("Composite numbers in the range 1 to 10 are:");
let line = "";
for (let num = 1; num <= 10; num++) {
if (isComposite(num)) {
line += num + " ";
}
}
console.log(line.trim()); Same √n test, expressed with for (let i = 2; i * i <= number; i++). 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.
function firstFactor(number) {
if (number <= 1) {
return null;
}
let i = 2;
while (i * i <= number) {
if (number % i === 0) {
return i;
}
i += 1;
}
return null;
}
for (const n of [12, 7, 1, 9]) {
const f = firstFactor(n);
if (f === null) {
const label = n <= 1 ? "neither" : "prime";
console.log(n + ": not composite (" + label + ")");
} else {
console.log(n + ": composite (divisible by " + f + ")");
}
} Same loop as isComposite, 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 isComposite(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: condense-a-number warm-up after divisibility.
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 condense a number for another digit-manipulation warm-up.
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 integer 1 is neither prime nor composite. The smallest composite is 4 (2 × 2). Every composite n has a prime divisor p with p ≤ √n, which is why trial division only needs to reach √n.
Learn how to repeatedly sum digits until a single digit remains.
9 people found this page helpful