Table of contents
1.
Introduction 
2.
Smith Number
3.
Examples
3.1.
Example 1
3.2.
Example 2
3.3.
Example 3
4.
Steps to Find Smith Number
5.
Code Implementation of Smith Number in Java
5.1.
Java
6.
Frequently Asked Questions
6.1.
Can a prime number be a Smith number?
6.2.
Is every composite number a Smith number?
6.3.
Are there infinitely many Smith numbers?
7.
Conclusion
Last Updated: Aug 14, 2024
Easy

Smith Number in Java

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Smith numbers are a special type of number in mathematics. They are positive integers where the sum of the digits equals the sum of the digits in the prime factors of the number. 

Smith Number in Java

In this article, we will learn what Smith numbers are, look at some examples, and learn how to find them using a Java program. 

Smith Number

A Smith number is a composite number where the sum of its digits is equal to the sum of the digits in its prime factorization. 

Let's discuss this:


First, we need to find the prime factors of the number. These are the prime numbers that, when multiplied together, give us the original number. For example, the prime factors of 12 are 2 and 2 and 3 (2 x 2 x 3 = 12).


Next, we add up the digits of the original number. In the case of 12, we add 1 + 2 to get 3.


Then, we add up the digits of the prime factors. For 12, the prime factors are 2, 2, and 3. Adding these digits gives us 2 + 2 + 3 = 7.


If the sum of the digits of the original number (3 in this case) is equal to the sum of the digits of its prime factors (7), then the number is a Smith number.


So, 12 is not a Smith number because 3 is not equal to 7. But there are many other numbers that do satisfy this property, and those are the Smith numbers.

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

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.

Live masterclass