A Smith number is a composite integer whose digit sum equals the digit sum of its prime factors with multiplicity.
Remember
n is composite
AND DigitSum(n) == DigitSum of all prime factors (repeats count)
Examples: 4, 22, 27, 58, 85, 94
4 = 2 × 2 → 4 == 2 + 2
85 = 5 × 17 → 8+5 == 5 + (1+7)
27 = 3 × 3 × 3 → 2+7 == 3+3+3
n
Factors
Digit sum
Factor digits
Smith?
4
2 × 2
4
2 + 2 = 4
Yes
85
5 × 17
13
5 + 1 + 7 = 13
Yes
15
3 × 5
6
3 + 5 = 8
No
7
prime
—
—
No
Approach
How to Check a Smith Number in JavaScript
Five clear steps — then reuse the helpers to list Smith numbers in a range.
Reject n ≤ 1 (not composite).
Factorize n with trial division (keep factors with multiplicity).
If only one prime factor appears once, n is prime → not Smith.
Compare DigitSum(n) with the digit sum of every factor.
Equal sums → Smith; otherwise not.
Helper
Job
Time
digitSum
Sum digits of one integer
O(log n)
Factorize
Collect prime factors with repeats
O(√n)
isSmith
Composite + matching sums
O(√n)
Pseudocode
Pseudocode
function DigitSum(x):
s = 0
while x > 0:
s = s + (x % 10)
x = x / 10
return s
function IsSmith(n):
if n <= 1: return false
original = n
factorDigitSum = 0
factorCount = 0
while n % 2 == 0:
factorDigitSum = factorDigitSum + DigitSum(2)
factorCount = factorCount + 1
n = n / 2
f = 3
while f * f <= n:
while n % f == 0:
factorDigitSum = factorDigitSum + DigitSum(f)
factorCount = factorCount + 1
n = n / f
f = f + 2
if n > 1:
factorDigitSum = factorDigitSum + DigitSum(n)
factorCount = factorCount + 1
if factorCount == 1: return false
return DigitSum(original) == factorDigitSum
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Rule
Composite and DigitSum(n) == FactorDigitSum
Multiplicity
Count repeated factors (e.g. three 3’s in 27)
Primes
Never Smith
Smallest
4
Classic yes
4, 22, 27, 58, 85, 94
Classic no
1, 7 (prime), 15 (sums differ)
Try it
Live Preview
Enter an integer to factorize it and compare digit sums.
Whole numbers only. Tap a chip or type a value.
Result
n = 85
Factors: 5 × 17
DigitSum(85) = 13
Factor digits = 5 + 1 + 7 = 13
85 is a Smith number
Code
JavaScript Programs
Build digitSum and isSmith, list Smith numbers in a range, then trace both sums side by side. Each example has a Try it Yourself editor.
Example 1 — isSmith Check
Factorize with multiplicity, reject primes, then compare digit sums.
JavaScript
function digitSum(x) {
let s = 0;
while (x > 0) {
s += x % 10;
x = Math.floor(x / 10);
}
return s;
}
function isSmith(n) {
if (n <= 1) {
return false;
}
const original = n;
let factorDigitSum = 0;
let factorCount = 0;
while (n % 2 === 0) {
factorDigitSum += digitSum(2);
factorCount++;
n = Math.floor(n / 2);
}
for (let f = 3; f * f <= n; f += 2) {
while (n % f === 0) {
factorDigitSum += digitSum(f);
factorCount++;
n = Math.floor(n / f);
}
}
if (n > 1) {
factorDigitSum += digitSum(n);
factorCount++;
}
if (factorCount === 1) {
return false; // prime
}
return digitSum(original) === factorDigitSum;
}
const number = 85;
if (isSmith(number)) {
console.log(number + " is a Smith number");
} else {
console.log(number + " is not a Smith number");
}
digitSum peels off each last digit with % 10 and Math.floor.
Even factors are peeled with the while (n % 2 === 0) loop.
Odd trial factors continue until f * f > n.
A leftover n > 1 is itself a prime factor.
factorCount === 1 means the original value was prime → reject.
Otherwise compare digit sums; for 85 both sides are 13.
Example 2 — Smith Numbers in a Range
Reuse isSmith to print every Smith number from 1 to 100.
JavaScript
function digitSum(x) {
let s = 0;
while (x > 0) {
s += x % 10;
x = Math.floor(x / 10);
}
return s;
}
function isSmith(n) {
if (n <= 1) {
return false;
}
const original = n;
let factorDigitSum = 0;
let factorCount = 0;
while (n % 2 === 0) {
factorDigitSum += digitSum(2);
factorCount++;
n = Math.floor(n / 2);
}
for (let f = 3; f * f <= n; f += 2) {
while (n % f === 0) {
factorDigitSum += digitSum(f);
factorCount++;
n = Math.floor(n / f);
}
}
if (n > 1) {
factorDigitSum += digitSum(n);
factorCount++;
}
if (factorCount === 1) {
return false;
}
return digitSum(original) === factorDigitSum;
}
console.log("Smith numbers from 1 to 100:");
let line = "";
for (let i = 1; i <= 100; i++) {
if (isSmith(i)) {
line += i + " ";
}
}
console.log(line.trim());
Same helpers as Example 1 keep the rule in one place.
The outer loop visits every integer from 1 to 100.
Only values that pass isSmith are collected.
Primes and unmatched composites are skipped automatically.
Up to 100 the classic list is 4 22 27 58 85 94.
Example 3 — Trace S(n) and F(n)
Print both digit sums so you can see why a value passes or fails.
JavaScript
function digitSum(x) {
let s = 0;
while (x > 0) {
s += x % 10;
x = Math.floor(x / 10);
}
return s;
}
function factorDigitSum(n) {
let total = 0;
let x = n;
while (x % 2 === 0) {
total += digitSum(2);
x = Math.floor(x / 2);
}
for (let f = 3; f * f <= x; f += 2) {
while (x % f === 0) {
total += digitSum(f);
x = Math.floor(x / f);
}
}
if (x > 1) {
total += digitSum(x);
}
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 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);
}
4: S=4, F=4 -> Smith
7: S=7, F=7 -> not Smith
15: S=6, F=8 -> not Smith
27: S=9, F=9 -> Smith
85: S=13, F=13 -> Smith
How It Works
S is the digit sum of n; F is the digit sum of its prime factors.
Primes like 7 can have S === F, but isPrime still rejects them.
15 shows a clear mismatch: 6 vs 8.
4 and 27 match and are composite → Smith.
Edit candidates in the Try it Yourself editor to explore more values.
Edge Cases & Pitfalls
Check these before calling the solution done.
Primes
Always reject
If factorCount === 1, the number is prime — not Smith.
Repeats
Count multiplicity
For 27, add digit sum of 3 three times.
Multi-digit
Factor digits too
For 17 use 1 + 7, not the bare value 17.
1
Not composite
Return false for n ≤ 1 before factoring.
Analysis
Time and Space Complexity
Program
Time
Extra space
isSmith (trial factors)
O(√n)
O(1)
Smith in range 1..N
O(N√N)
O(1)
Trace candidates
O(c√n) for c values
O(1)
Digit summing is cheap next to factorization. For interview sizes, trial division is enough.
Remember
Key Takeaways
Definition: composite + matching digit sums of number and prime factors.
Multiplicity: repeated factors count every time.
Primes: never Smith — exclude with a factor count of 1.
Test set: 85 → yes; 27 → yes; 15 → no; 7 → no.
One line: if n is composite and its digit sum matches the digit sum of its prime factors, n is Smith.
Frequently Asked Questions
A Smith number is a composite integer whose digit sum equals the digit sum of all its prime factors, counting repeats. Examples: 4, 22, 27, 58, 85, 94.
By definition primes are excluded. Otherwise every prime would pass, because its only prime factor is itself.
Yes. For 27 = 3 × 3 × 3, the factor digit sum is 3 + 3 + 3 = 9, matching 2 + 7.
Yes. 4 = 2 × 2. Digit sum is 4 and factor digit sum is 2 + 2 = 4.
Trial factorization up to √n is O(√n). Listing Smith numbers from 1 to N by checking each value is about O(N√N).
Use the Try it Yourself links under each code sample — they open an in-browser editor with the same logic so you can edit the input and Run.