Wednesday, August 20, 2008

Enjoying my time with BITS (all puns intended)

So, after keeping this blog as a just an unfilled diary page for a large part of the semester, I am finally back to my blog.I really don't know what keeps on attracting me back to this page, but i guess it must be the sense of belonging to this whole space of mine. I had always planned to fill up my blog with posts atleast every week, but that couldn't materialise for the lack of ideas and initiative on my part as I am quite a lazy fellow. So, here i am today on 20th August , 08, sitting infront of my lappy and writing this post at completely ungodly hour of 4:30 am. I had long debated what to put up in my first post, with different ideas hitting me every time. But the most exciting development of the day(or shud I say yesterday????) has been my development of the code for the golomb series. Now, I realise that I may be taken as just another computer freak but I must say that to us, the programming beginners, the joy of surmounting even the tiniest of our problems is immense and it is in that pleasure that I am drunken today. Well, what happened is quite simple, I had developed some good codes at home during my vacations, but my great laptop which has of late developed a RAM problem, had to be formatted and i forgot to take any backup. That made me lose all my efforts of 2 and a half months.but no problems, our brain is perhaps a better hard disk than anything else. So I am currently in the process of recovering and modyfying all, what i say, my 'lost codes'. So I am precisely doing this thing at 10 pm (yesterday) when a friend from IIT Kanpur shows up. Now, this guy is a serious Java freak and even though I don't want to be drawn into an algorithm development discussion, yet i cannot resist doing so. So, now the talk veers to Golomb series, I doubted whether i had heard this word ever before. Even Googling doesn't render much help here.That may well be a part of the reason why i am writing this post.Someday, someone gonna dig this out and he (preferably she) may end up writing me a letter of thanx :). But before clouds of doubt clutter my thinking (i think i am being too grandiloquent, oops soorry!!) a mail with an attachment of the friend's assignment reaches me and i am on the go. the Golomb series turned out be rather simple and as the attachment says is pretty self explanatory. i must say that i have a special place for codes and sequences in my heart. I love finding patterns and structures in seemingly unconnected data and it has become quite a hobby with me. Now more about Golomb series. This is the only non-decreasing sequence of positive integers with the property that it contains exactly f(k) occurrences of k for each k. Sounds nerdy??? well blame it on IITs and IItians :D. The series is something like this:

n 1 2 3 4 5 6 7 8 9 10 11 12
f(n) 1 2 2 3 3 4 4 4 5 5 5 6

now the question was to develop a program to generate this sequence for a given 'n'taking time factors into consideration. As we can all see, there are some repetition sequences already there. but i envisaged a different set of patterns to generate the code for this series. First of all, we can see thatfor every n that is a perfect square, the f(n) is an odd numberwhich can be depicted as i + 2 if i = 1 for n = 1.
e.g. : when n=1, f(n)= 1.
when n=4, f(n)= 3. and so on...
Secondly, f(n) at a value of 'n' that is a perfect square repeats itself square root(n) number of times.

Lastly, between two 'n' values that are perfect squares there are larger square - (smaller square + 1) values of 'n' and f(n).

Now, thats all one needs to know to develop a very efficient solution to this problem. Here i present mine written in C++ language. though i agree that i have not added comments but  still you can make it out easily.Lastly i will agree that though this code is not the shortest one (there is infact a three line code that can use three for() loops instead) but certainly this is the one with the shortest time complexity :

 #include 
class abc{
 private :
  int per_sq(int u)
  {
int x;
for(x=1;x<=u;x++)
{
if((u-(x*x))==0)
return x;
}
return 0;
  }
  int near_sq(int v)
  {
 do{
v++;
 }while(per_sq(v)>0);
     return v;
  }
void golomb(int a)
{
int i, j, k, c;
i = 0;j = 1;
for(j=1;j<=a;)
{
c = per_sq(j);
if(c>0)
{
i++;
for(k=0;k
{
if(j<=a)
{
cout <<>
j++;
}
else
break;
}
}
else
{
 i++;
 for(k=0;k
 {
 if(j<=a)
 {
 cout << i ;
 j++;
 }
 else
break;
 }
}
}
}
 public :
  void get()
  {
 int n;
 cout << "Enter the value of n : ";
 cin >> n;
 golomb(n);
  }
};
void main()
{
 abc obj;
 obj.get();
}



1 comment:

Neeraj Agarwal said...

Nice work BBT... way to go ahead.. u are really fodu in getting the patterns out of the series.. all the very very best for your blog and ur future too..