# Smith Number in Java

In this section, we will learn what is a smith number and also create Java program to check if the given number is smith or not. The smith number program frequently asked in Java coding tests and academics.

## Smith Number

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.Sum of digits of n = Sum of digits of prime factors of n (counted with multiplicity)

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

1. Read or initialize a number from the user.
2. Find the sum of its digits.
3. Find the prime factors of the given number.
4. Determine the sum of digits of its prime factors.
5. 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

1. import java.util.*;
2. public class SmithNumberExample1
3. {
4. //function finds the sum of digits of its prime factors
5. static int findSumPrimeFactors(int n)
6. {
7. int i=2, sum=0;
8. while(n>1)
9. {
10. if(n%i==0)
11. {
12. sum=sum+findSumOfDigit(i);
13. n=n/i;
14. }
15. else
16. {
17. do
18. {
19. i++;
20. }
21. while(!isPrime(i));
22. }
23. }
24. //returns the sum of digits of prime factors
25. return sum;
26. }
27. //function finds the sum of digits of the given numbers
28. static int findSumOfDigit(int n)
29. {
30. //stores the sum
31. int s=0;
32. while(n>0)
33. {
34. //finds the last digit of the number and add it to the variable s
35. s=s+n%10;
36. //removes the last digit from the given number
37. n=n/10;
38. }
39. //returns the sum of digits of the number
40. return s;
41. }
42. //function checks if the factor is prime or not
43. static boolean isPrime(int k)
44. {
45. boolean b=true;
46. int d=2;
47. while(d<Math.sqrt(k))
48. {
49. if(k%d==0)
50. {
51. b=false;
52. }
53. d++;
54. }
55. return b;
56. }
57. //driver code
58. public static void main(String args[])
59. {
60. Scanner sc = new Scanner(System.in);
61. System.out.print(“Enter a number: “);
62. //reads an integer from the user
63. int n=sc.nextInt();
64. //calling the user-defined function that finds the sum of digits of the given number
65. int a = findSumOfDigit(n);
66. //calling the user-defined function that finds the sum of prime factors
67. int b = findSumPrimeFactors(n);
68. System.out.println(“Sum of Digits of the given number is = “+a);
69. System.out.println(“Sum of digits of its prime factors is = “+b);
70. //compare both the sums
71. if(a==b)
72. //prints if above condition returns true
73. System.out.print(“The given number is a smith number.”);
74. //prints if above condition returns false
75. else
76. System.out.print(“The given number is not a smith number.”);
77. }
78. }

Output 1:

Output 2:

Let’s create a Java program that finds all the smith numbers up to 10,000.

SmithNumberExample2.java

1. import java.util.*;
2. public class SmithNumberExample2
3. {
4. //function finds all the prime factors for the given number
5. static List<Integer> findPrimeFactors(int n)
6. {
7. //creating an array list that stores the prime factors
8. List<Integer> result = new ArrayList<>();
9. for (int i = 2; n % i == 0; n = n/i)
11. for (int i = 3; i * i <= n; i =i+2)
12. {
13. while (n % i == 0)
14. {
16. n = n/i;
17. }
18. }
19. if (n != 1)
20. //the add() method adds the prime factors to the list
22. return result;
23. }
24. //function finds the sum of digits of the given number
25. static int sumOfDigits(int n)
26. {
27. int sum = 0;
28. while (n > 0)
29. {
30. //finds the last digit and add it to the sum
31. sum =sum+(n % 10);
32. //removes the last digit from the number
33. n = n/10;
34. }
35. //returns the sum of digits
36. return sum;
37. }
38. //driver code
39. public static void main(String args[])
40. {
41. //finds all smit numbers up to 10000
42. for (int n = 1; n < 10000; n++)
43. {
44. //calling the user-defined function that finds prime factors
45. List<Integer> factors = findPrimeFactors(n);
46. //the size() method returns the number of elements in the list
47. //executes until the condition becomes false
48. if (factors.size() > 1)
49. {
50. int sum = sumOfDigits(n);
51. for (int f : factors)
52. sum =sum-sumOfDigits(f);
53. if (sum == 0)
54. System.out.println(n);
55. }
56. }
57. }
58. }

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
```