Check Smith Number in Python

Beginner
⏱️ 12 min read
📚 Updated: May 2026
🎯 2 Code Examples
Number theory

What you'll learn

  • The definition of a Smith number (composite + matching digit sums).
  • How to factor with trial division and count prime factors with multiplicity.
  • How to check one number and list all Smith numbers from 1 to 100.

Overview

A Smith number is a composite integer where digit sum of the number equals digit sum of its prime factors (counting repeats).

Prerequisites

Loops, modulo, integer division, and basic prime factorization.

What is a Smith number?

Let S(n) be digit sum of n and F(n) be digit sum of all prime factors of n with multiplicity. If n is composite and S(n) = F(n), then n is Smith.

Example 22: S(22)=2+2=4; factorization 22 = 2*11, so F(22)=2+(1+1)=4.

Why multiplicity matters

For 27 = 3*3*3, you must count each 3. So factor digit sum is 3+3+3=9, which matches 2+7=9.

Quick examples

85 is Smith. 7 is not (prime).

Live preview

Use n >= 1. Preview is capped at 10^12.

Live result
Press "Run check".

Algorithm

1

Reject non-composite

If n <= 1 or n is prime, not Smith.

2

Compute S(n)

Sum digits of n.

3

Compute F(n)

Factor n and add digit sums of factors with multiplicity.

4

Compare

Smith iff S(n) equals F(n).

📜 Pseudocode

Pseudocode
function isSmith(n):
  if n <= 1 or isPrime(n): return false
  return digitSum(n) == factorDigitSum(n)
1

Check a single number

python
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.")
2

Smith numbers from 1 to 100

python
# Reuse helpers above
print("Smith Numbers in the Range 1 to 100:")
for i in range(1, 101):
    if is_smith(i):
        print(i, end=" ")
print()
📤 Output
Smith Numbers in the Range 1 to 100:
4 22 27 58 85 94

Efficiency notes

Trial division is fine for interview-size inputs.

Composite gate is mandatory to avoid false positives on primes.

❓ FAQ

A Smith number is a composite number where the digit sum of the number equals the digit sum of all prime factors (counting repeats).
By definition primes are excluded. Otherwise every prime would trivially pass.
Yes. 4 = 2 * 2. Digit sum(4) = 4 and factor digit sum = 2 + 2 = 4.
Yes. 85 = 5 * 17. 8+5 = 13 and 5 + (1+7) = 13.
No. 7 is prime, and primes are excluded.
Yes. For 27 = 3 * 3 * 3, digit sum of factor 3 is counted three times.

🔄 Input / output examples

nSmith?Note
85Yes5 * 17, both sums = 13
7NoPrime
58Yes2 * 29
15No6 vs 8

Edge cases and pitfalls

n = 1

Not Smith

Not composite.

Prime n

Always reject

Definition requires composite.

Multiplicity

Must count repeats

For 27, count 3 three times.

⏱️ Time and space complexity

StepTimeSpace
digit_sumO(log n)O(1)
factor_digit_sumO(sqrt(n))O(1)
range 1..Uabout O(U*sqrt(U))O(1)

Summary

  • Smith means composite + matching digit sums.
  • Count repeated prime factors.
  • From 1 to 100: 4 22 27 58 85 94.
Did you know?

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

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.

9 people found this page helpful