Smith Number in Java

In this section, we will explore the concept of Smith numbers and develop Java programs to determine whether a given number is a Smith number. The topic of Smith numbers often appears in Java coding tests and academic assignments. Our focus will be on understanding the characteristics of Smith numbers and implementing code to check if a given number qualifies as a Smith number.

What is a Smith Number?

A Smith number, also known as a joke number, is a composite number that possesses the unique property of having the sum of its digits equal to the sum of the digits of its prime factors, excluding the number 1. Smith numbers can be found in the sequence OEIS A006753. In our exploration, we will delve into the characteristics of Smith numbers and develop Java programs to determine whether a given number qualifies as a Smith number.

Sum of digits of n = Sum of digits of prime factors of n (counted with multiplicity)

Smith Number Example

Example 1: Suppose, we want to check whether the number 85 is smith or not.

The sum of digits = 8 + 5 = 13

Prime factors of 85 are 5,17

The 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 whether another number 999 is Smith or not.

The Sum of digits = 9+ 9+9 = 27

Prime factors of 999 are: 3×3×3,37

The 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.

Steps to Find Smith Number

  1. Read or assign a value to the variable representing the number from the user.
  2. Calculate the sum of the digits of the given number.
  3. Find the prime factors of the given number.
  4. Determine the sum of the digits of the prime factors.
  5. Compare the sum of the digits of the given number with the sum of the digits of its prime factors.
  6. If the sums are equal, then the given number is a Smith number.
  7. Otherwise, it is not a Smith number.

Smith Number Program in Java

SmithNumberExample.java

import java.util.*;  
public class SmithNumberExample  
{  
//function finds the sum of digits of its prime factors  
static int findSumPrimeFactors(int n)  
{  
int i=2, sum=0;  
while(n>1)  
{  
if(n%i==0)  
{  
sum=sum+findSumOfDigit(i);  
n=n/i;  
}  
else  
{  
do  
{  
i++;  
}  
while(!isPrime(i));  
}  
}  
//returns the sum of digits of prime factors  
return sum;  
}  
//function finds the sum of digits of the given numbers  
static int findSumOfDigit(int n)  
{  
//stores the sum  
int s=0;  
while(n>0)  
{  
//finds the last digit of the number and add it to the variable s      
s=s+n%10;  
//removes the last digit from the given number  
n=n/10;  
}  
//returns the sum of digits of the number  
return s;  
}  
//function checks if the factor is prime or not  
static boolean isPrime(int k)  
{  
boolean b=true;  
int d=2;  
while(d<Math.sqrt(k))  
{  
if(k%d==0)  
{  
b=false;  
}  
d++;  
}  
return b;  
}  
//driver code  
public static void main(String args[])  
{  
Scanner sc = new Scanner(System.in);  
System.out.print("Enter a number: ");  
//reads an integer from the user  
int n=sc.nextInt();  
//calling the user-defined function that finds the sum of digits of the given number  
int a = findSumOfDigit(n);  
//calling the user-defined function that finds the sum of prime factors  
int b = findSumPrimeFactors(n);  
System.out.println("Sum of Digits of the given number is = "+a);  
System.out.println("Sum of digits of its prime factors is = "+b);  
//compare both the sums  
if(a==b)  
//prints if above condition returns true  
System.out.print("The given number is a smith number.");  
//prints if above condition returns false  
else  
System.out.print("The given number is not a smith number.");  
}  
}

Output 1:

Enter a number: 265
Sum of Digits of the given number is = 13
Sum of digits of its prime factors is = 13
The given number is a smith number.

Output 2:

Enter a number: 2805
Sum of Digits of the given number is = 15
Sum of digits of its prime factors is = 18
The given number is not a smith number.

SmithNumberExample1.java

import java.util.*;  
public class SmithNumberExample1   
{  
//function finds all the prime factors for the given number  
static List<Integer> findPrimeFactors(int n)   
{  
//creating an array list that stores the prime factors      
List<Integer> result = new ArrayList<>();  
for (int i = 2; n % i == 0; n = n/i)  
result.add(i);  
for (int i = 3; i * i <= n; i =i+2)   
{  
while (n % i == 0)   
{  
result.add(i);  
n = n/i;  
}  
}  
if (n != 1)  
//the add() method adds the prime factors to the list  
result.add(n);  
return result;  
}  
//function finds the sum of digits of the given number  
static int sumOfDigits(int n)   
{  
int sum = 0;  
while (n > 0)   
{  
//finds the last digit and add it to the sum      
sum =sum+(n % 10);  
//removes the last digit from the number  
n = n/10;  
}  
//returns the sum of digits  
return sum;  
}  
//driver code      
public static void main(String args[])   
{  
//finds all smit numbers up to 10000      
for (int n = 1; n < 10000; n++)   
{  
//calling the user-defined function that finds prime factors  
List<Integer> factors = findPrimeFactors(n);  
//the size() method returns the number of elements in the list  
//executes until the condition becomes false  
if (factors.size() > 1)   
{  
int sum = sumOfDigits(n);  
for (int f : factors)  
sum =sum-sumOfDigits(f);  
if (sum == 0)  
System.out.println(n);  
}  
}  
}  
}

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
1842
1858
1872
1881
1894
1903
1908
1921
1935
1952
1962
1966
2038
2067
2079
2155
2173
2182
2218
2227
2265
2286
2326
2362
2366
2373
2409
2434
2461
2475
2484
2515
2556
2576
2578
2583
2605
2614
2679
2688
2722
2745
2751
2785
2839
2888
2902
2911
2934
2944
2958
2964
2965
2970
2974
3046
3091
3138
3168
3174
3226
3246
3258
3294
3345
3366
3390
3442
3505
3564
3595
3615
3622
3649
3663
3690
3694
3802
3852
3864
3865
3930
3946
3973
4054
4126
4162
4173
4185
4189
4191
4198
4209
4279
4306
4369
4414
4428
4464
4472
4557
4592
4594
4702
4743
4765
4788
4794
4832
4855
4880
4918
4954
4959
4960
4974
4981
5062
5071
5088
5098
5172
5242
5248
5253
5269
5298
5305
5386
5388
5397
5422
5458
5485
5526
5539
5602
5638
5642
5674
5772
5818
5854
5874
5915
5926
5935
5936
5946
5998
6036
6054
6084
6096
6115
6171
6178
6187
6188
6252
6259
6295
6315
6344
6385
6439
6457
6502
6531
6567
6583
6585
6603
6684
6693
6702
6718
6760
6816
6835
6855
6880
6934
6981
7026
7051
7062
7068
7078
7089
7119
7136
7186
7195
7227
7249
7287
7339
7402
7438
7447
7465
7503
7627
7674
7683
7695
7712
7726
7762
7764
7782
7784
7809
7824
7834
7915
7952
7978
8005
8014
8023
8073
8077
8095
8149
8154
8158
8185
8196
8253
8257
8277
8307
8347
8372
8412
8421
8466
8518
8545
8568
8628
8653
8680
8736
8754
8766
8790
8792
8851
8864
8874
8883
8901
8914
9015
9031
9036
9094
9166
9184
9193
9229
9274
9276
9285
9294
9296
9301
9330
9346
9355
9382
9386
9387
9396
9414
9427
9483
9522
9535
9571
9598
9633
9634
9639
9648
9657
9684
9708
9717
9735
9742
9760
9778
9840
9843
9849
9861
9880
9895
9924
9942
9968
9975
9985

To enhance your comprehension of Java concepts like Smith Number in Java, we recommend visiting tutorials.freshersnow.com.