Thursday, February 20, 2014

Nested Loop - III



Home Prime


The home prime of an integer that greater than 1, when prime number generated by repeatedly concatenate the prime factors including the repetitions.

Example :

Home Prime of 10 = 773
     No              Prime Factors          Concatenated to         is Prime
     10                     2,5                            25                          false
     25                     5                               55                          false
     55                     5,11                          511                        false
     511                   7,73                          773                        true

773 is a prime number so, home prime of 10 is 773.



Source Code


import java.io.*;
    class HomePrime
        {
            public static void main(String args[])throws IOException
                {
                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\tEnter a no. ");
                    int n=Integer.parseInt(br.readLine());
                    HomePrime HP=new HomePrime();
                    HP.fact(n);
                }  

            void fact(int n)
                {
                    String s;
                    int c=0,tmp=0,num=n;
                    while(true)
                        {
                            s="";
                            c=0;
                            for(int i=2;i<=n/2;i++)
                                {
                                    if(n%i==0)
                                        {
                                            if(isPrime(i))
                                                {
                                                    s=s+i;
                                                    c++;
                                                    tmp=i;
                                                }
                                        }
                                }
                            if(c==1)
                                {
                                    s=s+tmp;
                                }
                            if(isPrime(Integer.parseInt(s)))
                                {
                                    System.out.println("\n\tHome Prime ["+num+"] is = "+s);
                                    break;
                                }
                            else
                                {
                                    n=Integer.parseInt(s);
                                } 
                        }
                }

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



Output


     Enter a no. 10

     Home Prime [10] is = 773


Special Number / Krishnamurthy number


A number is a special number is sum of the factorial of each digit is equal to the number itself.

Example :

145 = 1! + 4! + 5! = 1 + 24 + 120 = 145



Source Code


import java.io.*;
    class Special_No
        {
            int num;
            int fact(int n)
                {
                    int p=1;
                    for(int i=1;i<=n;i++)
                        {   
                            p*=i;
                        }
                    return p;
        }
        boolean check()
            {
                int n=num;
                int sum=0;
                while(n>0)
                    {
                        sum+=fact(n%10);
                        n=n/10;
                    }

               if(sum==num)
                    {
                        return true;              
                    }

               else
                    {
                        return false;
                    }
                   

            }
            public static void main(String args[])throws IOException
                {
                    System.out.print("\n\t INPUT A NATURAL NUMBER ");
                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                    Special_No obj=new Special_No();
                    obj.num=Integer.parseInt(br.readLine());
                    if(obj.check())
                        {
                            System.out.println("\n\t"+obj.num+" is a special number");
                        }

                    else
                        {
                            System.out.println("\n\t"+obj.num+" is not a special number");
                        }

                }
        }



Output




     INPUT A NATURAL NUMBER 145

     145 is a special number

No comments: