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 ………………..
Examples:
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
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 of the factors (3+5+5+(6+5+8+3+7)) = 42
Sum of the digits are (4+9+3+7+7+7+5) = 42
Sum of the digits of the 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.
Sample data:
Input 94 Output SMITH Number
Input 102 Output NOT SMITH Number
Input 666 Output SMITH Number
Input 999 Output NOT SMITH Number
import
java.io.*;
class
Smith
{
static
BufferedReader br=
new
BufferedReader(
new
InputStreamReader(System.in));
//function for finding sum of digits
int
sumDig(
int
n)
{
int
s=
0
;
while
(n>
0
)
{
s=s+n%
10
;
n=n/
10
;
}
return
s;
}
//function for generating prime factors and finding their sum
int
sumPrimeFact(
int
n)
{
int
i=
2
, sum=
0
;
while
(n>
1
)
{
if
(n%i==
0
)
{
sum=sum+sumDig(i);
//Here 'i' is the prime factor of 'n' and we are finding its sum
n=n/i;
}
else
i++;
}
return
sum;
}
public
static
void
main(String args[])
throws
IOException
{
Smith ob=
new
Smith();
System.out.print(
"Enter a Number : "
);
int
n=Integer.parseInt(br.readLine());
int
a=ob.sumDig(n);
// finding sum of digit
int
b=ob.sumPrimeFact(n);
//finding sum of prime factors
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"
);
}
}
Output:
1. Enter a Number : 94
Sum of Digit = 13
Sum of Prime Factor = 13
It is a Smith Number
2. Enter a Number : 102
Sum of Digit = 3
Sum of Prime Factor = 13
It is Not a Smith Number
3. Enter a Number : 4937775
Sum of Digit = 42
Sum of Prime Factor = 42
It is a Smith Number
No comments:
Post a Comment