Must Be Composite
Gate
Primes are excluded by definition.
A Smith number is a composite integer whose digit sum equals the digit sum of its prime factors with multiplicity. Examples: 4, 22, 27, 58, 85, 94. Non-examples: primes (7), and composites like 15 where the sums do not match. This tutorial covers helpers for digit sum and factorization, a live check, worked JavaScript examples, edge cases, and complexity.
Gate
Primes are excluded by definition.
Digit sum
Sum the digits of n itself.
Factor digits
Digit-sum every prime factor.
27 = 3³
Count each repeated factor.
Try 85 / 7
See S(n) and F(n) side by side.
2*2
4 = 2+2 matches digit sum 4.
A Smith number is a composite integer where the digit sum of the number equals the digit sum of all its prime factors, counting repeats. So for 85 = 5 * 17, both sides equal 13. For 27 = 3 * 3 * 3, factor digits are counted three times: 3+3+3 = 9, matching 2+7.
Interviews expect a composite gate plus trial factorization. Without excluding primes, every prime would “pass” because its only prime factor is itself.
It combines primality, factorization, and digit arithmetic — a strong interview warm-up after composite numbers.
Reject primes first.
Matching digit sums.
27 needs three 3’s.
4 22 27 58 85 94
In short: if n is composite and digitSum(n) === factorDigitSum(n), then n is Smith.
Given an integer n, decide whether it is a Smith number: composite with matching digit sums.
# 85 -> 5*17, S=13, F=13 Smith
# 27 -> 3*3*3, S=9, F=9 Smith (multiplicity)
# 7 -> prime not Smith
# 15 -> 3*5, S=6, F=8 not Smith | Item | Type | Description |
|---|---|---|
n | int | Value to test (n >= 1). |
| Return | bool | true when composite and S(n) = F(n). |
| S(n) / F(n) | int | Digit sum of n / of prime factors. |
function isSmith(n) {
if (n <= 1 || isPrime(n)) {
return false;
}
return digitSum(n) === factorDigitSum(n);
} | Method | Idea | Notes |
|---|---|---|
| Trial factorization | Pull factors, sum their digits | Interview default |
| Range scan | Call isSmith on each i | Lists 4 22 27 58 85 94 |
| Trace with S/F | Print both sums for candidates | Great for debugging |
| Goal | Pattern |
|---|---|
| Digit sum | total += n % 10; n = Math.floor(n / 10) |
| Prime gate | if (n <= 1 || isPrime(n)) return false |
| Pull factor | while x % i === 0: total += digitSum(i) |
| Leftover prime | if x > 1: total += digitSum(x) |
| Verdict | digitSum(n) === factorDigitSum(n) |
| Range list | if (isSmith(i)) console.log(i) |
Same definition — different packaging.
isSmith(85)Full helpers + one verdict
1..100Classic interview listing
S / F printShows why yes or no
always falseComposite gate is mandatory
Reach for a Smith check when digit sums meet prime factorization.
After composite / prime-factor drills.
Find Smith values in a band.
27 forces repeated factors.
Shares helpers with Armstrong / condense.
Definition forbids them.
Key benefit: one memorable rule — composite + matching digit sums — with a clear factorization loop.
Computes S(n) and F(n), rejects primes, and reports the Smith verdict.
Three complete JavaScript programs — check 85, list Smith numbers from 1 to 100, and print S/F traces for several candidates. Click View Output to reveal sample console results.
Digit sum, primality, factor digit sum, then the composite gate.
Full helpers. Trial division is enough for interview-size inputs; the composite gate avoids false positives on primes.
function digitSum(n) {
let total = 0;
while (n > 0) {
total += n % 10;
n = Math.floor(n / 10);
}
return total;
}
function isPrime(n) {
if (n <= 1) {
return false;
}
if (n === 2) {
return true;
}
if (n % 2 === 0) {
return false;
}
for (let i = 3; i * i <= n; i += 2) {
if (n % i === 0) {
return false;
}
}
return true;
}
function factorDigitSum(n) {
let total = 0;
let x = n;
for (let i = 2; i * i <= x; i++) {
while (x % i === 0) {
total += digitSum(i);
x = Math.floor(x / i);
}
}
if (x > 1) {
total += digitSum(x);
}
return total;
}
function isSmith(n) {
if (n <= 1 || isPrime(n)) {
return false;
}
return digitSum(n) === factorDigitSum(n);
}
const number = 85;
console.log(isSmith(number) ? `${number} is a Smith Number.` : `${number} is not a Smith Number.`); 85 = 5 * 17. Digit sum is 8+5 = 13. Factor digit sum is 5 + (1+7) = 13, and 85 is composite, so it is Smith.
Reuse the helpers to list nearby Smith values.
Reuse the helpers from Example 1 and print matching values.
function digitSum(n) {
let total = 0;
while (n > 0) {
total += n % 10;
n = Math.floor(n / 10);
}
return total;
}
function isPrime(n) {
if (n <= 1) {
return false;
}
if (n === 2) {
return true;
}
if (n % 2 === 0) {
return false;
}
for (let i = 3; i * i <= n; i += 2) {
if (n % i === 0) {
return false;
}
}
return true;
}
function factorDigitSum(n) {
let total = 0;
let x = n;
for (let i = 2; i * i <= x; i++) {
while (x % i === 0) {
total += digitSum(i);
x = Math.floor(x / i);
}
}
if (x > 1) {
total += digitSum(x);
}
return total;
}
function isSmith(n) {
if (n <= 1 || isPrime(n)) {
return false;
}
return digitSum(n) === factorDigitSum(n);
}
console.log("Smith Numbers in the Range 1 to 100:");
let line = "";
for (let i = 1; i <= 100; i++) {
if (isSmith(i)) {
line += i + " ";
}
}
console.log(line.trim()); Within 1..100 the hits are 4, 22, 27, 58, 85, and 94. Memorizing this short list is a useful interview sanity check.
Print both sums so you can see why a value is Smith or not — including multiplicity for 27.
function digitSum(n) {
let total = 0;
while (n > 0) {
total += n % 10;
n = Math.floor(n / 10);
}
return total;
}
function isPrime(n) {
if (n <= 1) {
return false;
}
if (n === 2) {
return true;
}
if (n % 2 === 0) {
return false;
}
for (let i = 3; i * i <= n; i += 2) {
if (n % i === 0) {
return false;
}
}
return true;
}
function factorDigitSum(n) {
let total = 0;
let x = n;
for (let i = 2; i * i <= x; i++) {
while (x % i === 0) {
total += digitSum(i);
x = Math.floor(x / i);
}
}
if (x > 1) {
total += digitSum(x);
}
return total;
}
function isSmith(n) {
if (n <= 1 || isPrime(n)) {
return false;
}
return digitSum(n) === factorDigitSum(n);
}
const candidates = [4, 7, 15, 27, 85];
for (const n of candidates) {
const S = digitSum(n);
const F = factorDigitSum(n);
const label = isSmith(n) ? "Smith" : "not Smith";
console.log(`${n}: S=${S}, F=${F} -> ${label}`);
} 7 matches S and F but fails the composite gate. 15 is composite but 6 ≠ 8. 27 works only because F counts 3 three times.
Smith requires a composite n.
Sum the digits of n.
Factor n; add digit sums with multiplicity.
Equal means Smith; otherwise not.
Compare a two-factor Smith number, a repeated-factor Smith number, and a prime rejection.
| n | Factors | S(n) | F(n) | Verdict |
|---|---|---|---|---|
85 | 5 * 17 | 13 | 5+1+7=13 | Smith |
27 | 3 * 3 * 3 | 9 | 3+3+3=9 | Smith |
7 | prime | 7 | 7 | not Smith (gate) |
15 | 3 * 5 | 6 | 8 | not Smith |
Multiplicity and the composite gate are the two details interviewers listen for.
Where Smith checks show up beyond the interview prompt.
Composite + digit-sum factors.
Example: isSmith(85).
Find Smith values in a band.
Example: 4 22 27 58 85 94.
Trial division with repeats.
Example: 27 = 3³.
Shares helpers with condense / Armstrong.
Example: digitSum.
Print S and F for odd failures.
Example: Example 3.
Continue the interview chain.
Example: related CTA.
Pro Tip: open with “composite and S(n)=F(n) with multiplicity” before coding.
Why this factorization-plus-digit-sum approach works well.
Composite gate + two digit sums.
digitSum and isPrime appear elsewhere.
The inner while counts repeats naturally.
Print S and F to debug any n.
Pro Tip: dry-run 27 out loud — if you forget multiplicity, the answer collapses.
Small habits that keep Smith solutions interview-ready.
Return false before comparing sums.
Use while x % i === 0, not if.
17 contributes 1+7, not 17.
If x > 1 after the loop, add digitSum(x).
Expect 4 22 27 58 85 94.
Pro Tip: test 4, 7, 15, 27, and 85 — if those five behave, your logic is solid.
Mistakes that commonly break Smith-number programs.
S and F match for every prime.
→ Reject primes before comparing.
Counting 27 as a single 3.
→ Loop while divisible.
Using 17 instead of 1+7.
→ Always digitSum each factor.
Forgetting x > 1 after trial division.
→ Add digitSum(x) when needed.
1 is not composite.
→ Return false for n <= 1.
Handle these before claiming the check is complete.
Not composite.
Definition requires composite.
For 27, count 3 three times.
2 * 2, both sums = 4.
S = 6, F = 8.
17 → 1+7, not 17.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
4 22 27 58 85 94.Quick Takeaway: n is Smith when it is composite and digitSum(n) === factorDigitSum(n).
| Step | Time | Extra space |
|---|---|---|
| digitSum | O(log n) | O(1) |
| factorDigitSum | O(sqrt(n)) | O(1) |
| range 1..U | about O(U * sqrt(U)) | O(1) |
Dominant cost is trial factorization; digit summing is cheap by comparison.
A Smith number is composite with matching digit sums between the number and its prime factors (counting repeats). Gate primes, factor carefully, and compare S(n) with F(n).
Practice the three examples above, then continue to condensing a number.
Composite + S(n)=F(n) with multiplicity.
Classify composites whose digit sums match their factors.
must be composite
DefinitionS(n) = F(n)
Rulecount multiplicity
Factors4..94 in 1..100
CheckO(√n)
AnalysisSmith numbers are named after Albert Wilansky’s brother-in-law Harold Smith, who noticed 4937775 has this digit-sum property. The smallest is 4 (2 × 2: digit sum 4, factor-digit sum 2 + 2). Primes are never Smith numbers by definition.
Learn how to repeatedly sum digits until a single digit remains.
9 people found this page helpful