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 PHP 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 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 <= intdiv($n, $i) | Standard interview check |
| Show a factor | Return first divisor found | Great for explaining why |
| Goal | Pattern |
|---|---|
| Guard n ≤ 1 | if ($n <= 1) return false |
| √n loop | for ($i = 2; $i <= intdiv($n, $i); $i++) |
| Divisor hit | if ($n % $i === 0) return true |
| Range filter | if (isCompositeSqrt($i)) echo $i |
| 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 PHP 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.
<?php
function isCompositeSqrt(int $n): bool
{
if ($n <= 1) return false;
for ($i = 2; $i <= intdiv($n, $i); $i++) {
if ($n % $i === 0) return true;
}
return false;
}
$num = 12;
echo isCompositeSqrt($num)
? "$num is a composite number."
: "$num is not a composite number.";
?> Values ≤ 1 return false immediately. The loop stops at $i <= intdiv($n, $i) (same as $i * $i <= $n) 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.
<?php
function isCompositeSqrt(int $n): bool
{
if ($n <= 1) return false;
for ($i = 2; $i <= intdiv($n, $i); $i++) {
if ($n % $i === 0) return true;
}
return false;
}
echo "Composite numbers in the range 1 to 10 are:\n";
for ($i = 1; $i <= 10; $i++) {
if (isCompositeSqrt($i)) echo $i . " ";
}
?> Same √n test using the integer-safe bound $i <= intdiv($n, $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.
<?php
function firstFactor(int $n): ?int
{
if ($n <= 1) return null;
for ($i = 2; $i <= intdiv($n, $i); $i++) {
if ($n % $i === 0) return $i;
}
return null;
}
foreach ([12, 7, 1, 9] as $n) {
$f = firstFactor($n);
if ($f === null) {
$label = ($n <= 1) ? "neither" : "prime";
echo "$n: not composite ($label)\n";
} else {
echo "$n: composite (divisible by $f)\n";
}
}
?> Same loop as isCompositeSqrt, 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 <= intdiv($n, $i).
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 isCompositeSqrt($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 $i <= intdiv($n, $i) (or $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).
Using float sqrt($n) and casting can miss a factor near perfect squares.
→ Prefer $i <= intdiv($n, $i).
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