Python Smith Number Program

Beginner
7 min read
Updated: Oct 2026
2 programs
Live preview

What Is a Smith Number?

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
nFactorsDigit sumFactor digitsSmith?
42 × 242 + 2 = 4Yes
855 × 17135 + 1 + 7 = 13Yes
153 × 563 + 5 = 8No
7prime——No

How to Check a Smith Number in Python

Five clear steps — then reuse the helpers to list Smith numbers in a range.

  1. Reject n ≤ 1 (not composite).
  2. Factorize n with trial division (keep factors with multiplicity).
  3. If only one prime factor appears once, n is prime → not Smith.
  4. Compare digit_sum(n) with the digit sum of every factor.
  5. Equal sums → Smith; otherwise not.
HelperJobTime
digit_sumSum digits of one integerO(log n)
FactorizeCollect prime factors with repeatsO(√n)
is_smithComposite + matching sumsO(√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

    // peel off factor 2
    while n % 2 == 0:
        factorDigitSum = factorDigitSum + DigitSum(2)
        factorCount = factorCount + 1
        n = n / 2

    // odd factors
    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:   // leftover prime
        factorDigitSum = factorDigitSum + DigitSum(n)
        factorCount = factorCount + 1

    if factorCount == 1: return false   // prime
    return DigitSum(original) == factorDigitSum

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
RuleComposite and digit_sum(n) == factor_digit_sum
MultiplicityCount repeated factors (e.g. three 3’s in 27)
PrimesNever Smith
Smallest4
Classic yes4, 22, 27, 58, 85, 94
Classic no1, 7 (prime), 15 (sums differ)

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

Worked Walkthrough — n = 85

Factorize, sum digits on both sides, then decide.

Calculation
85 = 5 × 17  (composite)

DigitSum(85) = 8 + 5 = 13
Factor digits = DigitSum(5) + DigitSum(17)
              = 5 + (1 + 7) = 13

13 == 13 → 85 is Smith

Also try 27 = 3 × 3 × 3:
DigitSum(27) = 9
Factor digits = 3 + 3 + 3 = 9 → Smith

Counterexample 15 = 3 × 5:
1 + 5 = 6, but 3 + 5 = 8 → not Smith

Python Programs

Build digit_sum and is_smith, then list Smith numbers in a small range.

Example 1 — is_smith Check

Factorize with multiplicity, reject primes, then compare digit sums.

python
def digit_sum(x: int) -> int:
    s = 0
    while x > 0:
        s += x % 10
        x //= 10
    return s


def is_smith(n: int) -> bool:
    if n <= 1:
        return False

    original = n
    factor_digit_sum = 0
    factor_count = 0

    while n % 2 == 0:
        factor_digit_sum += digit_sum(2)
        factor_count += 1
        n //= 2

    f = 3
    while f * f <= n:
        while n % f == 0:
            factor_digit_sum += digit_sum(f)
            factor_count += 1
            n //= f
        f += 2

    if n > 1:
        factor_digit_sum += digit_sum(n)
        factor_count += 1

    if factor_count == 1:
        return False  # prime

    return digit_sum(original) == factor_digit_sum


number = 85
print(f"{number} is a Smith number" if is_smith(number) else f"{number} is not a Smith number")

How It Works

  1. digit_sum peels off each last digit with % 10.
  2. Even factors are peeled with the while n % 2 == 0 loop.
  3. Odd trial factors continue until f * f > n.
  4. A leftover n > 1 is itself a prime factor.
  5. factor_count == 1 means the original value was prime → reject.
  6. Otherwise compare digit sums; for 85 both sides are 13.

Example 2 — Smith Numbers in a Range

Reuse is_smith to print every Smith number from 1 to 100.

python
def digit_sum(x: int) -> int:
    s = 0
    while x > 0:
        s += x % 10
        x //= 10
    return s


def is_smith(n: int) -> bool:
    if n <= 1:
        return False

    original = n
    factor_digit_sum = 0
    factor_count = 0

    while n % 2 == 0:
        factor_digit_sum += digit_sum(2)
        factor_count += 1
        n //= 2

    f = 3
    while f * f <= n:
        while n % f == 0:
            factor_digit_sum += digit_sum(f)
            factor_count += 1
            n //= f
        f += 2

    if n > 1:
        factor_digit_sum += digit_sum(n)
        factor_count += 1

    if factor_count == 1:
        return False

    return digit_sum(original) == factor_digit_sum


start, end = 1, 100
print(f"Smith numbers from {start} to {end}:")
for i in range(start, end + 1):
    if is_smith(i):
        print(i, end=" ")
print()

How It Works

  1. Same helpers as Example 1 keep the rule in one place.
  2. The outer loop visits every integer from start to end.
  3. Only values that pass is_smith are printed.
  4. Primes and unmatched composites are skipped automatically.
  5. Up to 100 the classic list is 4 22 27 58 85 94.

Edge Cases & Pitfalls

Check these before calling the solution done.

Primes

Always reject

If factor_count == 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.

Time and Space Complexity

ProgramTimeExtra space
is_smith (trial factors)O(√n)O(1)
Smith in range 1..NO(N√N)O(1)

Digit summing is cheap next to factorization. For interview sizes, trial division is enough.

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).

Next: Strong Number

Check whether a number equals the sum of its digit factorials.

Strong number tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful