Thursday, March 1, 2012

Hailstone sequences - Easy one

Problem
=================================================
Start with x,
    if x is even --> x becomes half
    if x is off --> x becomes triple plus one
Sequence is believed to end with '1' all the time (hailstone sequence)
=================================================

Solution
============
I decided to put my newly learnt skills of Python to use for this easy one. :)

Here it goes:

   def hailstone_seq(x):
       print x,
       while x > 1:
           if x % 2 == 0:
               x = x/2
           else:
               x = 3*x + 1
           print x,
    
 

Friday, September 30, 2011

Hamming Numbers

This exercise is taken from www.programmingpraxis.com
=========================================================
"The sequence of Hamming numbers 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32, 36, … (A051037) consists of all numbers of the form 2i·3j·5k where i, j and k are non-negative integers. Edsger Dijkstra introduced this sequence to computer science in his book A Discipline of Programming, and it has been a staple of beginning programming courses ever since. Dijkstra wrote a program based on three axioms:

Axiom 1: The value 1 is in the sequence.

Axiom 2: If x is in the sequence, so are 2 * x, 3 * x, and 5 * x.

Axiom 3: The sequence contains no other values other than those that belong to it on account of Axioms 1 and 2.

Your task is to write a program to compute the first thousand terms of the Hamming sequence. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below."

=========================================================

Here is my C solution

=========================================================

#include 
#include 

int ham_num(int x) {
    int h = x;
    while (h && (h%2 == 0)) 
        h = h/2;

    while (h && (h%3 == 0)) 
        h = h/3;
    
    while (h && (h%5 == 0)) 
        h = h/5;
   /* 
    * h will be 1, when it is ham num, else it will be  
    * something else - example h =10  
    */
   return h;
}

int main(int argc, char *argv[])
{
    int x = 1, cnt = 1000;
    printf("Hamming Numbers : \n");
    if (argc > 1)  
        cnt = atoi(argv[1]); 

    while (cnt) {
        if (ham_num(x) == 1) {
            printf("%d ",x);
            cnt--;
        }   
        x++;
    }   
    printf("\n");
}

Wednesday, August 24, 2011

Snapper Chain!! - Google codejam-2010

http://code.google.com/codejam/contest/dashboard?c=433101#s=p0

You can find the problem description above! Believe me it's so much of fun to solve this!!
Go ahead and try!!!! I could not believe after I got the solution!.....Ufffff.......

Hint :: Think in bits!!

Approach ::
Lets say we have 2 snappers (N =2)-
Initially both are in OFF state

Remember : With a snap, only the snapper receiving power changes state!!!

OFF OFF Initial State
ON OFF After snap k =1
OFF ON After snap k =2
ON ON After snap k =3
OFF OFF Snap k =4

See the bit pattern ?? Lets say OFF = 0; and ON = 1

0 0 Initial State K=0
1 0 K=1
0 1 K=2
1 1 K=3
0 0 K=4

So, if there are N snappers, we will turn the light on when K = [2 (power) N ] - 1
In the above case, N = 2, K = pow(2,2)-1 = 3. , After that, a snap will get us to the initial state!!

Isn't it simple and great!!!!!!!!

===================================================

int main () {
   int n,k,num_cases;
   int factor,i;
   FILE *fp;

   fp = fopen("A-large-practice.in","r");
   if (!fp) {
      exit(0);
   }
   fscanf(fp,"%d",&num_cases);
   for (i = num_cases ; i > 0; i--) {
      fscanf(fp,"%d %d", &n, &k);
      factor = pow(2,n);

      printf("Case #%d: ",num_cases-i+1);
      if (k%factor == (factor-1))
          printf("ON\n");
      else
          printf("OFF\n");

   }
 fclose(fp);
}