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 Python 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 digit_sum(n) == factor_digit_sum(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 or isPrime(n):
return false
return digitSum(n) == factorDigitSum(n) | Method | Idea | Notes |
|---|---|---|
| Trial factorization | Pull factors, sum their digits | Interview default |
| Range scan | Call is_smith 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 //= 10 |
| Prime gate | if n <= 1 or is_prime(n): return False |
| Pull factor | while x % i == 0: total += digit_sum(i) |
| Leftover prime | if x > 1: total += digit_sum(x) |
| Verdict | digit_sum(n) == factor_digit_sum(n) |
| Range list | if is_smith(i): print(i) |
Same definition — different packaging.
is_smith(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 Python 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.
def digit_sum(n: int) -> int:
total = 0
while n > 0:
total += n % 10
n //= 10
return total
def is_prime(n: int) -> bool:
if n <= 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
i = 3
while i * i <= n:
if n % i == 0:
return False
i += 2
return True
def factor_digit_sum(n: int) -> int:
total = 0
x = n
i = 2
while i * i <= x:
while x % i == 0:
total += digit_sum(i)
x //= i
i += 1
if x > 1:
total += digit_sum(x)
return total
def is_smith(n: int) -> bool:
if n <= 1 or is_prime(n):
return False
return digit_sum(n) == factor_digit_sum(n)
number = 85
print(f"{number} is a Smith Number." if is_smith(number) else f"{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.
# Reuse digit_sum, is_prime, factor_digit_sum, is_smith from Example 1
print("Smith Numbers in the Range 1 to 100:")
for i in range(1, 101):
if is_smith(i):
print(i, end=" ")
print() 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.
# Reuse digit_sum, is_prime, factor_digit_sum, is_smith from Example 1
candidates = [4, 7, 15, 27, 85]
for n in candidates:
S = digit_sum(n)
F = factor_digit_sum(n)
label = "Smith" if is_smith(n) else "not Smith"
print(f"{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: is_smith(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: digit_sum.
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.
digit_sum and is_prime 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 digit_sum(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 digit_sum each factor.
Forgetting x > 1 after trial division.
→ Add digit_sum(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 digit_sum(n) == factor_digit_sum(n).
| Step | Time | Extra space |
|---|---|---|
| digit_sum | O(log n) | O(1) |
| factor_digit_sum | 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 property. The smallest is 4. Primes are never Smith numbers by definition.
Learn how to repeatedly sum digits until a single digit remains.
9 people found this page helpful