Thursday, January 16, 2014

Powerful & Smith Number


Smith Number

Smith numbers are the numbers whose  sum of  digits is equal to the sum of the digits of its prime factor.
Example, 666  
prime factor (2,3,3,37) = 2+3+3+3+7 = 18
sum of digits 6 + 6 + 6 = 18.



//4, 22,  319, 346,  636,666,1086

import java.io.*;
    class Smith_No
        {
            public static void main(String args[])throws IOException
                {
                    Smith_No sm=new Smith_No();
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    int n,num,s,sum=0,f,i=2;
                    System.out.print("\n\tEnter a Number : ");
                    n=Integer.parseInt(Br.readLine());
                    num=n;
                    s=sm.sep(n);
                    while(n>1)
                        {
                            f=0;
                            if(n%i==0)
                                {
                                    n=n/i;
                                    if(sm.prime(i))
                                        {
                                            sum=sum+sm.sep(i);;
                                        }    
                                }
                            else
                                {
                                    i++;
                                }
                            }       
                    if(s==sum)
                        {
                            System.out.println("\n\t"+num+" is a Smith Number"); 
                        }
                    else
                        {
                            System.out.println("\n\t"+num+" is not a Smith Number");
                        }
                }

           boolean prime(int n)
                {
                    int i;
                    for(i=2;i<=n/2;i++)
                        {
                            if(n%i==0)
                            return false;
                        }
                    return true;
                }

           int sep(int n)
                {
                    int s=0;
                    while(n>0)
                        {
                            s=s+n%10;
                            n=n/10;
                        }
                    return s;
                }
        }


Output

          Enter a Number : 666
          666 is a Smith Number

          Enter a Number : 456
          456 is Not a Powerful Number


Powerful Number


A powerful number is a positive integer that the square of each prime factor also be a factor of the number.
Example. 9
prime factor of 9 = 3 where 32 also a factor of the number
factor of 81 is 3,9 and 32 and 92 also facor of 81.



//4, 8, 9, 16, 100, 225, 243, 972, 1000

import java.io.*;
    public class Powerful_No
        {
            public static void main(String args[]) throws IOException
                {
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    int n,i=2,j;
                    Powerful_No PN=new Powerful_No();
                    boolean flag=true;
                    System.out.print("\n\tEnter a Number : ");
                    n=Integer.parseInt(Br.readLine());
                    for(j=2;j<=n/2;j++)
                        {
                            if(n%j==0)
                                {
                                    if(PN.isPrime(j))
                                        {
                                            if(n%(j*j)!=0)
                                                {
                                                    flag=false;
                                                    break;
                                                }
                                        }
                                }
                       }
                    if(flag)
                        {
                            System.out.print("\n\t"+n+" is a Powerful Number");
                        }
                    else
                        {
                            System.out.print("\n\t"+n+" is Not a Powerful Number");
                        }
                }
   // Checking for Prime Numbers
  boolean isPrime(int n)
     {
       int i;
       for(i=2;i<=n/2;i++)
        {
          if(n%i==0)
            return false;
         }
       return true;
    }
  }


Output


          Enter a Number : 81
          81 is a Powerful Number


          Enter a Number : 38
          38 is Not a Powerful Number

No comments: