Examples
Example 1
Number: 4
Prime factorization: 2 x 2
Sum of digits in the number: 4
Sum of digits in prime factors: 2 + 2 = 4
4 is a Smith number because the sum of its digits (4) equals the sum of the digits in its prime factors (4).
Example 2
Number: 666
Prime factorization: 2 x 3 x 3 x 37
Sum of digits in the number: 6 + 6 + 6 = 18
Sum of digits in prime factors: 2 + 3 + 3 + 3 + 7 = 18
666 is a Smith number because the sum of its digits (18) equals the sum of the digits in its prime factors (18).
Example 3
Number: 965
Prime factorization: 5 x 193
Sum of digits in the number: 9 + 6 + 5 = 20
Sum of digits in prime factors: 5 + 1 + 9 + 3 = 18
965 is not a Smith number because the sum of its digits (20) does not equal the sum of the digits in its prime factors (18).
Steps to Find Smith Number
To check whether a given number is a Smith number or not, we will follow below mentioned steps:
1. Find the sum of the digits in the number.
- Break the number into its individual digits.
- Add all the digits together.
2. Find the prime factorization of the number.
- Determine all the prime factors of the number.
- Write the number as a product of its prime factors.
3. Find the sum of the digits in the prime factorization.
- Break down each prime factor into its individual digits.
- Add all the digits from the prime factors together.
4. Compare the sums.
- If the sum of the digits in the number equals the sum of the digits in its prime factorization, it is a Smith number.
- If the sums are not equal, it is not a Smith number.
Now, let's apply these steps to the number 729:
1. Sum of digits in 729: 7 + 2 + 9 = 18
2. Prime factorization of 729: 3 x 3 x 3 x 3 x 3 x 3 (or 3^6)
3. Sum of digits in prime factorization: 3 + 3 + 3 + 3 + 3 + 3 = 18
4. Compare the sums: 18 (sum of digits in 729) = 18 (sum of digits in prime factorization)
Therefore, 729 is a Smith number.
Code Implementation of Smith Number in Java
Java
import java.util.Scanner;
public class SmithNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
scanner.close();
if (isSmithNumber(number)) {
System.out.println(number + " is a Smith number.");
} else {
System.out.println(number + " is not a Smith number.");
}
}
public static boolean isSmithNumber(int number) {
int sumOfDigits = sumOfDigits(number);
int sumOfPrimeFactors = sumOfPrimeFactors(number);
return sumOfDigits == sumOfPrimeFactors;
}
public static int sumOfDigits(int number) {
int sum = 0;
while (number > 0) {
sum += number % 10;
number /= 10;
}
return sum;
}
public static int sumOfPrimeFactors(int number) {
int sum = 0;
for (int i = 2; i <= number; i++) {
while (number % i == 0) {
sum += sumOfDigits(i);
number /= i;
}
}
return sum;
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Enter a number: 6
6 is not a Smith number.
Let’s understand the code now :
1. We import the `Scanner` class to read input from the user.
2. In the `main` method:
- We create a `Scanner` object to read input from the user.
- We prompt the user to enter a number and store it in the `number` variable.
- We close the scanner to prevent resource leaks.
- We call the `isSmithNumber` method to check if the number is a Smith number.
- Based on the result, we print whether the number is a Smith number or not.
3. The `isSmithNumber` method:
- Takes an integer `number` as input.
- Calculates the sum of digits in the number using the `sumOfDigits` method.
- Calculates the sum of digits in the prime factors of the number using the `sumOfPrimeFactors` method.
- Returns `true` if the sums are equal (indicating a Smith number), and `false` otherwise.
4. The `sumOfDigits` method:
- Takes an integer `number` as input.
- Calculates the sum of digits in the number by repeatedly extracting the last digit using `%` and adding it to the `sum` variable.
- Returns the sum of digits.
5. The `sumOfPrimeFactors` method:
- Takes an integer `number` as input.
- Iterates from 2 to `number` to find prime factors.
- If `i` is a prime factor of `number`, it repeatedly divides `number` by `i` and adds the sum of digits of `i` to the `sum` variable.
- Returns the sum of digits in the prime factors.
Frequently Asked Questions
Can a prime number be a Smith number?
No, a prime number cannot be a Smith number because Smith numbers are composite numbers.
Is every composite number a Smith number?
No, not every composite number is a Smith number. Only those numbers that satisfy the Smith number property are considered Smith numbers.
Are there infinitely many Smith numbers?
Yes, there are infinitely many Smith numbers, just like there are infinitely many composite numbers.
Conclusion
In this article, we explained the concept of Smith numbers, which are composite numbers whose sum of digits equals the sum of the digits in their prime factorization. We looked at examples to understand how Smith numbers work and learned the steps to identify them. We also implemented a Java program to check whether a given number is a Smith number or not.
You can also check out our other blogs on Code360.