Thursday 19 January 2017

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

Example :
1.    666
Prime factors are 2, 3, 3, and 37
Sum of the digits are (6+6+6)=18
Sum of 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 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 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



Solution:-

import java.util.*;
class Smith
{
     int number;
     static Scanner sc=new Scanner(System.in);
     void input()
     {
           System.out.println("Enter A Number");
           number=sc.nextInt();
     }
     int sum(int n)
     {
           int s=0;
           while (n!=0)
           {
                s=s+n%10;
                n=n/10;
           }
           return s;
     }
     void isSmith()//check for smith no.
     {
           int s1,s2=0;
           int cp=number;
           s1=sum(number);
           for(int i=2;cp!=1;i++)
           {
                while(cp%i==0)
                {
                     s2=s2+sum(i);
                     cp=cp/i;
                }
           }
           if(s1==s2)
           {
                System.out.println(number+" Is A Smith Number");
           }
           else
                System.out.println(number+" Is NOT A Smith Number");
     }
     public static void main(String args[])
     {
           Smith obj=new Smith();
           char ch='y';
           while(ch=='y')
           {
                obj.input();
                obj.isSmith();
                System.out.println("Do you want to continue Y/N");
                ch=sc.next().charAt(0);
           }
     }
}

1 comment:

Goldbach Number ISC 2018

Question: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes. Note: All even integer numbers ...