- S
8+5=13- F
5 + (1+7) = 13from5×17
Check Smith Number in JavaScript
What you’ll learn
- The definition of a Smith number (composite + matching digit sums).
- Why the first sample program on some sites is wrong if it forgets the composite rule.
- How to factor with trial division, add digit sums of each prime copy, and list Smith numbers up to 100.
Overview
A Smith number is a composite positive integer n such that the sum of the decimal digits of n equals the sum of the decimal digits of all primes in its prime factorization, counting multiplicity (so 4 = 2 × 2 contributes two twos).
Two programs
Check 85 (classic demo), then print every Smith number from 1 to 100.
Live preview
See digit sum, factor-digit sum, and whether n is composite—all in one box.
Correct definition
Primes are ruled out explicitly so answers match textbooks and OEIS-style lists.
Prerequisites
Loops, integer division, modulo, and the idea of prime factorization.
- Functions, loops, modulo operations, and comfort reading small JavaScript functions.
- Knowing composite means “not prime and greater than 1.”
What is a Smith number?
Compute S(n) = sum of decimal digits of n. Factor n into primes and compute F(n) = sum of the decimal digits of each prime factor, written as many times as that factor appears. If n is composite and S(n) = F(n), then n is a Smith number.
Example 22: digits 2 + 2 = 4. Factorization 2 × 11: digit sums 2 and 1 + 1 = 2, total 4. Match, and 22 is composite—so 22 is Smith.
Why multiplicity matters
The number 27 = 33 uses the prime 3 three times. So F(27) = digit-sum(3) + digit-sum(3) + digit-sum(3) = 9. The digit sum of 27 is 2 + 7 = 9, so 27 is Smith. If you only counted distinct primes once, you would get the wrong answer.
A short program that only checks S(n) == F(n) without testing compositeness will label every prime as Smith, because for prime p, F(p) is just the digit sum of p. Always require composite.
Quick examples
- Reason
- Prime numbers are excluded by definition.
Takeaway: always factor completely, count repeated primes, and keep the composite gate.
Live preview
Trial factorization in the browser (same idea as the C code). Very large inputs may be capped for speed.
Algorithm
Goal: return true if and only if n is a Smith number.
Reject small or prime values
If n ≤ 1 or n is prime, it cannot be Smith.
Compute S(n)
Repeatedly take n % 10 and divide by 10 until the copy is zero.
Compute F(n)
Trial-divide starting at 2. Each time p divides the working copy, add S(p) once and strip p. After the loop, if anything remains above 1, it is a large prime factor—add S of that too.
Compare
Smith if and only if S(n) == F(n) (we already know n is composite).
📜 Pseudocode
function digitSum(x):
s ← 0
while x > 0:
s ← s + (x mod 10)
x ← floor(x / 10)
return s
function factorDigitSum(n):
s ← 0
t ← n
p ← 2
while p * p ≤ t:
while t mod p = 0:
s ← s + digitSum(p)
t ← t / p
p ← p + 1
if t > 1:
s ← s + digitSum(t)
return s
function isSmith(n):
if n ≤ 1 or isPrime(n):
return false
return digitSum(n) = factorDigitSum(n)Check a single number
Uses i <= x / i in the factorization loop (no i * i overflow). Important: isSmith returns false for primes so 7, 13, etc. are never mislabeled.
function digitSum(n) {
let s = 0;
while (n > 0) {
s += n % 10;
n = Math.floor(n / 10);
}
return s;
}
function isPrime(n) {
if (n <= 1) return false;
if (n === 2) return true;
if (n % 2 === 0) return false;
for (let i = 3; i <= n / i; i += 2) {
if (n % i === 0) return false;
}
return true;
}
function factorDigitSum(n) {
let sum = 0;
let x = n;
for (let i = 2; i <= x / i; i++) {
while (x % i === 0) {
sum += digitSum(i);
x = Math.floor(x / i);
}
}
if (x > 1) sum += digitSum(x);
return sum;
}
function isSmith(n) {
if (n <= 1 || isPrime(n)) return false;
return digitSum(n) === factorDigitSum(n);
}
const number = 85;
if (isSmith(number)) {
console.log(number + " is a Smith Number.");
} else {
console.log(number + " is not a Smith Number.");
}Explanation
factor_digit_sum walks trial divisors. Each time it removes a prime i, it adds the digit sum of i (so 11 adds 1+1, not 11 as a single “digit”).
if (n <= 1 || isPrime(n)) return false;Definition guard. This is the line many short tutorials forget; without it, primes pass the digit-sum test by accident.
while (x % i == 0) { sum += digit_sum(i); x /= i; }Multiplicity. Powers like 3k add S(3) exactly k times.
Smith numbers from 1 to 100
Same helpers; the outer loop is a straight scan. Output matches the classic list.
function digitSum(n) {
let s = 0;
while (n > 0) {
s += n % 10;
n = Math.floor(n / 10);
}
return s;
}
function isPrime(n) {
if (n <= 1) return false;
if (n === 2) return true;
if (n % 2 === 0) return false;
for (let i = 3; i <= n / i; i += 2) {
if (n % i === 0) return false;
}
return true;
}
function factorDigitSum(n) {
let sum = 0;
let x = n;
for (let i = 2; i <= x / i; i++) {
while (x % i === 0) {
sum += digitSum(i);
x = Math.floor(x / i);
}
}
if (x > 1) sum += digitSum(x);
return sum;
}
function isSmith(n) {
if (n <= 1 || isPrime(n)) return false;
return digitSum(n) === factorDigitSum(n);
}
const values = [];
for (let i = 1; i <= 100; i++) {
if (isSmith(i)) values.push(String(i));
}
console.log("Smith Numbers in the Range 1 to 100:");
console.log(values.join(" "));Explanation
The range program is intentionally boring on purpose: one reliable isSmith, reused 100 times.
Efficiency notes
Factorization. Trial division up to √n is fine for interview sizes. Pollard Rho or sieves matter only for much larger cryptography-scale numbers.
Prime test. You can skip a separate isPrime if you track whether you stripped at least one factor and ended with x == 1 after factoring the original n—but a clear prime test is easier to read.
Interview: state the composite rule first, then explain multiplicity with a cube like 27.
❓ FAQ
🔄 Input / output examples
n | Smith? | Note |
|---|---|---|
85 | Yes | 5 × 17, digit sums match |
7 | No | Prime |
58 | Yes | 2 × 29 |
15 | No | 3 × 5: 1+5=6 vs 3+5=8 |
Edge cases and pitfalls
Most mistakes are definitional (forgetting composite) or implementation (not counting repeated factors).
Not Smith
Not composite; both isSmith paths should reject it quickly.
p2
Example 9 = 3 × 3: factor-digit sum is 3+3=6, digit sum of 9 is 9—not equal, so 9 is not Smith.
Loop bounds
Prefer i <= x / i over i * i <= x for large x on fixed-width int.
⏱️ Time and space complexity
| Step | Time (trial division) | Extra space |
|---|---|---|
digit_sum(n) | O(log n) digits | O(1) |
factor_digit_sum(n) | O(√n) worst case | O(1) |
isPrime(n) | O(√n) | O(1) |
Scan 1..U | about O(U√U) | O(1) |
Summary
- Definition: composite
nwith digit sum equal to sum of digit sums of prime factors with multiplicity. - Code: factor by trial division; never label primes as Smith.
- Classic list to 100:
4 22 27 58 85 94.
Smith 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.
9 people found this page helpful
