Smith Number in Java
In this section, we will learn what is a smith number and also create Java programs to check if the given number is smith or not. The smith number program frequently asked in Java coding tests and academics.
Smith Number
A Smith number is a composite number whose sum of digits equals to the sum of digits of its prime factors, excluding 1. It is also known as a joke number. It is a sequence of OEIS A006753.
Let’s understand it through an example.
Smith Number Example
Example 1: Suppose, we want to check the number 85 is smith or not.
Sum of digits = 8 + 5 = 13
Prime factors of 85 is: 5,17
Sum of digits of its prime factors = 5 + 1+ 7 = 13
Compare the sum of digits with the sum of digits of its prime factors i.e. 13=13. Both are equal. Hence, the given number (85) is a smith number.
Example 2: Let’s check another number 999 is smith or not.
Sum of digits = 9+ 9+9 = 27
Prime factors of 999 is: 3×3×3,37
Sum of digits of its prime factors = 3+3+3+3+7 =19
Compare the sum of digits with the sum of digits of its prime factors i.e. 27≠19. Hence, the given number (999) is not a smith number.
Similarly, we can check other numbers also. Some other smith numbers are 4, 27, 85, 94, 121, 166, 202, 265, 274, 319, 346, 666, etc.
Steps to Find Smith Number
- Read or initialize a number from the user.
- Find the sum of its digits.
- Find the prime factors of the given number.
- Determine the sum of digits of its prime factors.
- Compare the sum of digits with the sum of digits of its prime factors.
- If they are equal, the given number is a smith
- Else, not a smith number.
Let’s implement the above steps in a Java program.
Smith Number Java Program
SmithNumberExample1.java
Output 1:
Output 2:
Let’s create a Java program that finds all the smith numbers up to 10,000.
SmithNumberExample2.java
Output:
4, 22, 27, 58, 85, 94, 121, 166, 202, 265, 274, 319, 346, 355, 378, 382, 391, 438, 454, 483, 517, 526, 535, 562, 576, 588, 627, 634, 636, 645, 648, 654, 663, 666, 690, 706, 728, 729, 762, 778, 825, 852, 861, 895, 913, 915, 922, 958, 985, 1086, 1111, 1165, 1219, 1255, 1282, 1284, 1376, 1449, 1507, 1581, 1626, 1633, 1642, 1678, 1736, 1755, 1776, 1795, 1822
Note: In the output above, we have shown only a few smith numbers because there are a lot of smith numbers. So, try it yourself.