Smith Number in Java: Definition, Steps, Examples, and Practice Problems

Learn what a Smith Number is in Java. Understand its definition, rules, examples, and how to check a Smith Number using Java. Includes detailed explanation and 3 practice challenges for students.

Smith Number in Java: Definition, Steps, Examples, and Practice Problems

A Smith Number is a composite number whose sum of digits is equal to the sum of the digits of its prime factors (including repetition). Smith Numbers are an interesting concept from number theory and frequently appear in coding challenges involving prime factorization.

What Is a Smith Number?

A number is called a Smith Number if:

  • It is a composite number (not prime).
  • The sum of its digits equals the sum of digits of its prime factors.
  • Prime factors must be counted with repetition.

Example of a Smith Number

Consider the number 666:

  • Digits of 666 → 6 + 6 + 6 = 18
  • Prime factorization → 2 × 3 × 3 × 37
  • Digits of factors → (2) + (3) + (3) + (3 + 7) = 18

Since the sums match, 666 is a Smith Number.

Steps to Check a Smith Number

  1. Check if the number is composite (not prime).
  2. Find the sum of the digits of the number.
  3. Find all prime factors of the number (with repetitions).
  4. Find the sum of digits of each prime factor.
  5. Compare both sums.

Java Program to Check Smith Number


import java.util.*;

public class SmithNumber {

    // Function to check if a number is prime
    static boolean isPrime(int n) {
        if (n <= 1) return false;
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) return false;
        }
        return true;
    }

    // Function to find sum of digits
    static int digitSum(int n) {
        int sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = sc.nextInt();

        // Smith numbers must be composite
        if (isPrime(num)) {
            System.out.println("Not a Smith Number");
            return;
        }

        int originalDigitSum = digitSum(num);
        int factorDigitSum = 0;

        int temp = num;

        // Prime factorization
        for (int i = 2; i <= temp; i++) {
            while (temp % i == 0) {
                if (isPrime(i)) {
                    factorDigitSum += digitSum(i);
                }
                temp /= i;
            }
        }

        if (originalDigitSum == factorDigitSum) {
            System.out.println("Smith Number");
        } else {
            System.out.println("Not a Smith Number");
        }
    }
}

  

Examples

Number Prime Factors Digit Sum Factor Digit Sum Smith Number?
666 2, 3, 3, 37 18 18 Yes
728 2, 2, 2, 7, 13 17 17 Yes
85 5, 17 13 13 Yes
94 2, 47 13 13 Yes
27 3, 3, 3 9 9 Yes

3 Practice Challenges

Challenge 1: Find Smith Numbers in a Range

Write a Java program to print all Smith Numbers between 1 and 10,000.

Challenge 2: Check Multiple Numbers

Take an array of integers from the user and determine how many of them are Smith Numbers.

Challenge 3: Sum of Smith Numbers

Write a Java program to calculate the sum of all Smith Numbers present in a given list.