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

For Example:   N = 666.
    Prime factors are 2, 3, 3, 37.
    Sum of the digits = 6 + 6 + 6 = 18.
    Sum of the digits of the factors 2 + 3 + 3 + 3 + 7 = 18.
    Hence, 666 is a Smith Number.     
  

Some of the Smith Numbers are :  4, 22, 27, 58, 85, 94, 121,

import java.util.*;
class Smith_number
{
    public static void main(String[]args)
    {
        Scanner sc=new Scanner (System.in);
        System.out.println("Enter a Number");
        int n=sc.nextInt();
        int i,p=n,c,j,a,r,s=0,s1=0;
        // SUM OF DIGITS
        while(p>0)
        {
            s1+=p%10;
            p/=10;
        }
        System.out.println("Sum of Digits:"+s1);
        // SUM OF PRIME FACTORS
        p=n;
        for(i=1;i<=n;++i)
        {
            c=0;
            for(j=1;j<=i;++j)
                if(i%j==0)
                c++;
            if(c==2)
            {
                while(p%i==0)
                {
                    a=i;
                    r=0;
                    while(a>0)
                    {
                        r+=a%10;
                        a/=10;
                    }
                    s+=r;
                    p/=i;
                }// end of outer while-loop
            }// end of if block.
        }// end of for loop
        System.out.println("SUM OF PRIME FACTORS: "+s);
        if(s==s1)
            System.out.println("IT IS A SMITH NO.");
       else
            System.out.println("NOT A SMITH NO.");
    }
}

No comments

Powered by Blogger.