Check Smith Number in Java

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

What you’ll learn

  • Smith number rule: composite and S(n) == F(n).
  • How to compute digit sum of a number and factor digits.
  • Why prime numbers are excluded from Smith definition.
  • Range checking and live preview interpretation.

Overview

A Smith number is composite and has equal digit sums between the number and its prime factorization (with multiplicity). Example: 85 has digit sum 8+5=13, and factors 5 x 17 give 5 + (1+7)=13.

Two Java examples

Check one number and list Smith numbers up to 100.

Live preview

See both sums and the composite rule in one output.

Correct definition

Prime numbers are excluded even if digit sums match.

Prerequisites

Prime/composite basics, digit-sum function, and trial-division factorization.

  • Understand factor multiplicity (repeated primes count repeatedly).
  • Use integer loops safely with i <= x / i.

What is a Smith number?

A Smith number is a composite number whose own digit sum equals the digit sum of its prime factors (including repeated factors).

Primes are excluded by definition even if their digit sums would match trivially.

Math note

Let S(n) be sum of digits of n, and F(n) be sum of digits of prime factors with multiplicity. Smith condition is S(n) = F(n) with n composite.

Quick examples

85Smith
Check
13 = 5 + 8
27Smith
Factors
3 x 3 x 3
7Not
Reason
Prime (excluded)

Live preview

Trial factorization in the browser using the same logic as the Java code.

Use a positive integer. Preview is capped near 1012 for speed.

Live result
Press “Run check”.

Algorithm

Reject invalids

If n <= 1 or prime, return false.

Compute sums

Compute S(n) and factor-digit sum F(n).

Compare

If S(n) == F(n), it is Smith.

📜 Pseudocode

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

Check one number

java
public class Main {
    static int digitSum(int n) {
        int s = 0;
        while (n > 0) {
            s += n % 10;
            n /= 10;
        }
        return s;
    }

    static boolean isPrime(int n) {
        if (n <= 1) return false;
        if (n == 2) return true;
        if (n % 2 == 0) return false;
        for (int i = 3; i <= n / i; i += 2) {
            if (n % i == 0) return false;
        }
        return true;
    }

    static int factorDigitSum(int n) {
        int sum = 0;
        int x = n;
        for (int i = 2; i <= x / i; i++) {
            while (x % i == 0) {
                sum += digitSum(i);
                x /= i;
            }
        }
        if (x > 1) sum += digitSum(x);
        return sum;
    }

    static boolean isSmith(int n) {
        if (n <= 1 || isPrime(n)) return false;
        return digitSum(n) == factorDigitSum(n);
    }

    public static void main(String[] args) {
        int number = 85;
        if (isSmith(number)) {
            System.out.println(number + " is a Smith Number.");
        } else {
            System.out.println(number + " is not a Smith Number.");
        }
    }
}
2

Smith numbers in range 1 to 100

java
public class Main {
    static int digitSum(int n) {
        int s = 0;
        while (n > 0) {
            s += n % 10;
            n /= 10;
        }
        return s;
    }

    static boolean isPrime(int n) {
        if (n <= 1) return false;
        if (n == 2) return true;
        if (n % 2 == 0) return false;
        for (int i = 3; i <= n / i; i += 2) {
            if (n % i == 0) return false;
        }
        return true;
    }

    static int factorDigitSum(int n) {
        int sum = 0;
        int x = n;
        for (int i = 2; i <= x / i; i++) {
            while (x % i == 0) {
                sum += digitSum(i);
                x /= i;
            }
        }
        if (x > 1) sum += digitSum(x);
        return sum;
    }

    static boolean isSmith(int n) {
        if (n <= 1 || isPrime(n)) return false;
        return digitSum(n) == factorDigitSum(n);
    }

    public static void main(String[] args) {
        System.out.println("Smith Numbers in the Range 1 to 100:");
        for (int i = 1; i <= 100; i++) {
            if (isSmith(i)) {
                System.out.print(i + " ");
            }
        }
    }
}

Efficiency notes

Factorization. Trial division to √n is sufficient for interview-size inputs.

Composite guard. Reject primes first so the definition remains correct.

Implementation: use i <= x / i and one shared digit-sum helper.

❓ FAQ

A Smith number is a composite number where digit sum of n equals digit sum of prime factors (counting repeats).
If primes were allowed, every prime would pass trivially, so standard definition excludes primes.
Yes. 8+5=13 and factors are 5 and 17 whose digit sums are 5 and 8, total 13.
No. 7 is prime, and primes are excluded.
Multiplicity is part of prime factorization; for 27 = 3*3*3, you add digit sum of 3 three times.
With trial division for factors, one check is typically O(sqrt(n)) time and O(1) extra space.

Input/output examples

Input nOutput
85Smith number
27Smith number
7Not Smith (prime)

Edge cases and pitfalls

Most bugs come from skipping composite validation or not counting repeated factors.

n = 1

Not Smith

It is not composite and should be rejected immediately.

Prime powers

Multiplicity matters

For 27 = 3 x 3 x 3, include digit sum of 3 three times.

Overflow safety

Loop bound

Prefer i <= x / i over i * i <= x for large values.

⏱️ Time and space complexity

StepTimeExtra space
Prime checkO(√n)O(1)
Factor digit sumO(√n)O(1)
Single isSmithO(√n)O(1)

Summary

  • Definition: composite n with matching digit sums S(n) and F(n).
  • Code: trial division with multiplicity and prime rejection.
  • Known values to 100: 4, 22, 27, 58, 85, 94.
Did you know?

Small Smith numbers are 4, 22, 27, 58, 85, 94. 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