Thursday, February 27, 2014

Nested Loop - IV


//Magic Numbers : When the sum of digits of a number resultant to 1
//e.g. 19 = 1 + 9 = 10 = 1+ 0 = 1


import java.io.*;   
    class Magic_Happy
        {
            public static void main(String args[])throws IOException
                {
                    Magic_Happy MH=new Magic_Happy();
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    int n;
                    boolean flag;
                    System.out.print("\n\tEnter a number ");
                    n=Integer.parseInt(Br.readLine());
                    if(MH.Magic(n))
                        {
                            System.out.print("\n\t"+n+" is a Magic Number");
                        }
                    else
                        {
                            System.out.print("\n\t"+n+" is not a Magic Number");
                        }                       
                    if(MH.Happy(n))
                        {
                            System.out.print("\n\t"+n+" is a Magic Number");
                        }
                    else
                        {
                            System.out.print("\n\t"+n+" is not a Magic Number");
                        }  
                }
            boolean Magic(int n)
                {
                    int m=n,s=0;
                    while(m>9)
                        {
                            s=0;
                            while(m!=0)
                                {
                                    s=s+m%10;
                                    m=m/10;
                                }
                            m=s;   
                        }
                    if(s==1)
                        {
                            return true;
                        }
                    return false;   
               }


//when sum of the squares of its digits of a number is = 1
//Happy Numbers : 28 = 22 + 82 = 4 + 64 = 68 = 62 + 82 =100 =1

            boolean Happy(int n)
                {
                    int m=n,s=0;
                    while(m>9)
                        {
                            s=0;
                            while(m!=0)
                                {
                                    s=s+(int)Math.pow(m%10,2);
                                    m=m/10;
                                }
                            m=s;   
                        }
                    if(s==1)
                        {
                            return true;
                        }
                    return false;   
               }
        }



Output



          Enter a number 19

          19 is a Magic Number
          19 is a Happy Number

          Enter a number 37

          37 is a Magic Number
          37 is not a Happy Number



// A composite number that the sum of the number's digits equals the sum of the digits of its distinct prime factors.

// hoax numbers are 22, 58, 84, 85, 94, 136, 160, 166, 202, 234,


import java.io.*;
    class hoax
    {
        int n;
        hoax()
            {}
        hoax(int a)
            {
                n=a;
            }

            int digitsum(int n1)
                {
                    int r,s=0;
                    while(n1>0)
                        {
                            r=n1%10;
                            s=s+r;
                            n1=n1/10;
                        }
                    return  s;
                }

            int prime(int n2)
                {
                    int m,f1=0;
                    for(m=2;m
                        {
                            if(n2%m==0)
                                {
                                    return 0;
                                }
                        }
                    return 1;
                }
            void main()throws IOException
                {
                    int i,j,s1=0;
                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tHoax No : ");
                    int x=Integer.parseInt(br.readLine());
                    hoax ob=new hoax(x);
                    for(i=2;i
                        {
                            if(x%i==0 && ob.prime(i)==1)
                                {
                                    s1=s1+ob.digitsum(i);
                                }
                        }
                    if(s1==ob.digitsum(x))
                        {
                            System.out.println("\n\t"+x+" is a hoax number");
                        }
                    else
                        {
                            System.out.println("\n\t"+x+" is not a hoax number");
                        }
                }
            }    



Output



          Hoax No : 22

          22 is a hoax number
          Hoax No : 33

          33 is not a hoax number



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