A smith number is a composite number, the sum of whose digits is the sum of the digits of its prime factors obtained as a result of prime factorization (excluding 1).
The first few such numbers are 4, 22, 27, 58, 85, 94, 121
Example
1. 666
Prime factors are 2, 3, 3 and 37
Sum of the digits are (6+6+6) = 18
Sum of the digits of the factors (2+3+3+(3+7) = 18
2. 4937775
Prime factors are 3, 5, 5, 65837
Sum of the digits are (4+9+3+7+7+7+5) = 42
Sum of the digits (factors) (3+5+5+(6+5+8+3+7)=42
Write a program to input a number and display whether the number is a Smith number or not
import java.util.Scanner;
public class Smith
{
int n;
public Smith()
{
}
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("\nEnter a positive natural number ");
n=sc.nextInt();
}
int sumDig(int in)
{
int s=0;
while(in>0)
{
s=s+in%10;
in=in/10;
}
return s;
}
int sumPrimeFact()
{
int i=2, sum=0;
while(n>1)
{
if(n%i==0)
{
sum=sum+sumDig(i);
//Here 'i' is the prime factor of 'n'
n=n/i;
}
else
i++;
}
return sum;
}
public void isSmith()
{
int a=sumDig(n);// finding sum of digit
int b=sumPrimeFact(); //finding sum of primefactors
System.out.println("Sum of Digit = "+a);
System.out.println("Sum of Prime Factor = "+b);
if(a==b)
System.out.print("It is a Smith Number");
else
System.out.print("It is Not a Smith Number");
}
public static void main()
{
Smith obj = new Smith();
obj.input();
obj.isSmith();
}