Computer Science, asked by ManishRealHero, 11 months ago

some programs on smith numbers​

Answers

Answered by gurukulamdivya
1

Answer:

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

import java.util.*;

public class SmithNumbers

{

int sumDigit(int n)

{

int s=0;

while(n>0)

{

s=s+n%10;

n=n/10;

}

return s;

}

int sumPrimeFact(int n)

{

int i=2, sum=0;

while(n>1)

{

if(n%i==0)

{

sum=sum+sumDigit(i);  

n=n/i;

}

else

{

do{

i++;

}while(!IsPrime(i));

}

}

return sum;

}

boolean IsPrime(int k)

{

boolean b=true;

int d=2;

while(d<Math.sqrt(k))

{

if(k%d==0)

{

b=false;

}

d++;

}

return b;

}

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

SmithNumbers ob=new SmithNumbers();

System.out.print("Enter a Number : ");

int n=sc.nextInt();

int a=ob.sumDigit(n);

int b=ob.sumPrimeFact(n);

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:

Enter a Number : 85

Sum of Digit = 13

Sum of Prime Factor = 13

It is a Smith Number


ManishRealHero: itna lamba program
gurukulamdivya: yes
gurukulamdivya: but logic simple hai
ManishRealHero: iss se chota nahi hai
Answered by irakhedkar
0

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");

   }

}

Hope this helps,

ira.

Similar questions