Thursday, July 26, 2012

Miscellaneous – III – Keith No.


Miscellaneous – II – Keith No.

A Keith Number is an n-digit integer number 'N'.  It create a Fibonacci-like sequence.

1. Separate the digit of 'N' sum it.
2. Replace the first digit with second and so on.
3. Include the sum at the last position.
4. Sum the new set.
5. Continue the proces 2-4 till sum is less than 'N'.
6. If final sum comes equals to the 'N' then it is known as Keith Number.

Ex. 1.

14
1 + 4 = 5
4 + 5 =9
5 + 9 =14

Ex. 2.

197
 1 +   9 +     7 = 17
 9 +   7 +   17 = 33
 7 +  17 +   33 = 57
17 + 33 +   57 = 107
33 + 57 + 107 = 197

Some other Keith Numbers : 19, 28, 47, 61, 75, 742, 1104 1537  .

//Checking if a given number is a Vampire Number or not
#include <stdio.h>
#include <conio.h>

  void print(int a[],int dc)
      {
           int i;

           for(i=0;i<dc;i++)
              {
                   if(i<dc-1)
                        {
                            printf("%d + ",a[i]);
                        }
                   else
                        {
                            printf("%d ",a[i]);
                        }
              }
      }

  int digit(int n,int a[],int dc)
      {
           int i=dc-1,s=0;
           while(n>0)
              {
                   a[i--]=n%10;
                   s=s+n%10;
                   n=n/10;
              }
           printf("\n\t\t");
           print(a,dc);
           printf(" = %d",s);
           printf("\n\t\t");
           return s;
      }

  int dCount(int n)
      {
           int c=0;
           while(n>0)
              {
                   c++;
                   n=n/10;
              }
           return c;
      }

  void shift(int a[],int s,int dc)
      {
           int i;
           for(i=0;i<dc-1;i++)
               {
                   a[i]=a[i+1];
              }
           a[dc-1]=s;
      }

  int sumAr(int a[],int dc)
      {
           int i,s=0;
           for(i=0;i<dc;i++)
              {
                   s=s+a[i];
              }
           return s;
      }

  void main()
      {
           int i,dc,sum=0,n,*a;
           clrscr();
           printf("\n\tEnter a Number : ");
           scanf("%d",&n);
           sum=0;
           dc=dCount(n);
           a=(int *)malloc(dc*sizeof(int));
           sum=digit(n,a,dc);
           while(sum<n)
              {
                   shift(a,sum,dc);
                   sum=sumAr(a,dc);
                   printf("\n\t\t");
                   print(a,dc);
                   printf(" = %d",sum);
                   printf("\n\t\t");
              }
           if(sum==n)
              {
                   printf("\n\n\t\t%d is a Keith Number",n);
              }
           else
              {
                   printf("\n\n\t\t%d is not a Keith Number",n);
              }
           getch();
      }


Thursday, July 19, 2012

Miscellaneous – III – Vampire No.



Vampire Number :

A vampire number a composite natural number must consists even digit of numbers that can be divided ito two equal parts as x and y. Both parts are know as fangs and both cannot ends with trailing zeroes. A number can be considered a Vampire number when product of the fang produce the same number and it can be in any order.

Example: 1260 is a vampire number, with 21 and 60 as fangs, since 21 × 60 = 1260.
126000 = 210 × 600 is not, as both 210 and 600 have trailing zeroes.

Some other Vampire Numbers are : 1395, 1435, 1530, 1827, 2187, 6880, 102510.



//Checking if a given number is a Vampire Number or not

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <string.h>


int Digit(int n)
           {
              int c=0;
              while(n>0)
                   {
                        c++;
                        n/=10;
                   }
              return c;
           }
  void sort(char Kc[])
      {
           int i,j;
           char ch;
           for(i=0;i
              {
                   for(j=0;j
                        {
                            if(Kc[j]>Kc[j+1])
                                {
                                     ch=Kc[j];
                                     Kc[j]=Kc[j+1];
                                     Kc[j+1]=ch;
                                }
                        }
              }




//A series of Vampire Numbers

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <string.h>

int Digit(int n)
           {
              int c=0;
              while(n>0)
                   {

                        c++;
                        n/=10;

                   }
              return c;
           }
  void sort(char Kc[])
      {
           int i,j;
           char ch;
           for(i=0;i
              {
                   for(j=0;j
                        {
                            if(Kc[j]>Kc[j+1])
                                {
                                     ch=Kc[j];
                                     Kc[j]=Kc[j+1];
                                     Kc[j+1]=ch;
                                }
                        }
              }
      }
  void print(int a[],int i)
      {
           int j;
           print("\n\tVampire Numbers : \n"”);
           for(j=0;j
              {
                   printf("\n\t%d ",a[j]);
              }
      }
  void main()
      {
           int i,j,k,c,a[100],t=0,flag;
           char *kStr,*catStr,*tmp;
           clrscr();
           for(i=11; i<=100; i++)
              {
                   for(j=11; j<=100; j++)
                        {
                            flag=0;
                            c=Digit(i*j);
                            if(c%2!=0)
                                {
                                     continue;
                                }
                            if(i%10==0 && j%10==0)
                                {
                                     continue; //both fangs cannot have trailing '0'
                                }
                            k = i * j;
                            catStr[0]='\0';
                            tmp[0]='\0';
                            kStr[0]='\0';
                            itoa(k,kStr,10);
                            itoa(i,catStr,10);
                            itoa(j,tmp,10);
                            strcat(catStr,tmp);
                            sort(kStr);
                            sort(catStr);
                            if(strcmpi(kStr,catStr)==0)
                                {
                                     for(k=0;k
                                           {
                                                if(a[k]==i*j)
                                                    {
                                                          flag=1;
                                                          break;
                                                    }
                                           }
                                      if(flag==0)
                                           {
                                                a[t++]=i*j;
                                           }

                                }
                        }
              }
           print(a,t);
           getch();
      }