Happy Burthday

Yay Happy Birthday to myself :D
….. and Walt Disney? o_O

I turn 18 today :)

Lookie and Say

A friend of mine was trying to write an algorithm for the Look and Say Sequence. With nothing else to do, I thought I’d give it a try. To be very honest, Its probably the hardest thing I’ve written in C++. Hope I’ll be writing more and better code after my stupid exams :(

The sequence goes as follows: 1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211, …
where-
- 1 is read off as “one 1″ or 11.
- 11 is read off as “two 1’s” or 21.
- 21 is read off as “one 2, then one 1″ or 1211.
- 1211 is read off as “one 1, then one 2, then two 1’s” or 111221. etc

#include
using namespace std;

int main(int argc,char* argv[])
{

unsigned long i=1 ,r=0 ,m=1;
int count=0, countin=0;
while(count < 8) {
while(i!=0)
{

countin++;
if(r == 0) { r = m * (10 + i%10);
}
else if(i % 10 == (r/m) % 10) {
r = r + (m * 10);
}
else
{
m*=100;
r = r + (m * (10 + i%10 ));

}
i=i/10;
}
cout << r << ” , “;
i=r;
r=countin=0;
m=1;
count++;
}
cout << endl;
return 1;
}

Yeah, its limited to the size of “long”. Will change to strings when I can

Reblog this post [with Zemanta]