Showing posts with label Smith Number. Show all posts
Showing posts with label Smith Number. Show all posts

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

 * ex- 666 is a Smith Number -->prime factors --> 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

 */

import java.util.*;

class smith

{//class smith opens 

    int sumdigit(int n)//method to find the sum of the digits

    {

        int s=0;

        while(n>0)

        {

            s=s+(n%10);//10

            n=n/10;

        }

        return s;

    } 

    int sumprimefact(int n)//method to find the sum of the prime factors

    {

            int i=2,sum=0;

            while(n>1)//666>1 t

            {

                if(n%i==0)//666%2==0 t//37%37==0 t

                {

                    sum=sum+sumdigit(i);//0+2+3+3+10=18

                    n=n/i;

                }

                else

                i++;

            }

            return sum;

    }

    public static void main(String[]args)

    {//main method opens

            Scanner sc=new Scanner(System.in);

            smith obj=new smith();

            System.out.println("Enter a no");

            int n=sc.nextInt();//666

            int a=obj.sumdigit(n);//18

            int b=obj.sumprimefact(n);//18

            System.out.println("Sum of the digits= "+a);//18

            System.out.println("Sum of the prime factors= "+b);//18

            if(a==b)//18==18 true

            System.out.println("It is a smith no");

            else

            System.out.println("It is not a smith no");

    }//main method closes

}//class closes

    

 

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...