Pages

Tuesday, 29 November 2016

Smith number

 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
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
Write a program to input a number (<1000) and display whether the number is a Smith number or not.
Input specification:The first line contains the value for N
Output specification:
print “ n is a Smith number“ or “ n is not a Smith number” or “n is an Invalid input”
Sample Input 1:
666
Sample Output 1:
666 is a Smith number
Sample Input 2:
102
Sample Output 2:
102 is not a Smith number
Sample Input 3:
1024
Sample Output 3:
1024 is an Invalid input


Source Code:-

import java.util.*;

class Test{
    public static void main(String []args){
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        int i=2,m=n;
        int sum=0;
        int sumOfDig=sumOfDigit(n);
        if(n<1000){
            while(n>1){
                if(n%i==0){
                    sum=sum+sumOfDigit(i);
                    n=n/i;
                }
                else{
                    i++;
                }
            }
            if(sumOfDig==sum){
                System.out.println(m+" is a Smith number");
            }
            else{
                System.out.println(m+" is not a Smith number");
            }
        }
        else{
            System.out.println(n+" is an Invalid input");
        }
    }
    private static int sumOfDigit(int num){
        int sum=0;
        while(num>0){
            sum=sum+num%10;
            num/=10;
        }
        return sum;
    }
}

No comments:

Post a Comment