Friday, April 20, 2012

O(logn) for calculating fibonacci number

O(logn) code for computing fib(n)
 
#include
long int fib(long int n) {
 
    long int a=1, b=0, p=0, q=1, prev_a, prev_p = 0;
    while(n>0) {
        if (n%2 == 0) {
            prev_p = p;
            p = p*p + q*q;
            q = 2*prev_p*q + q*q;
            n /= 2;
        } else {
            prev_a = a;
            a = b*q + a*q + a*p;
            b = b*p + prev_a*q;
            n -= 1;
        }
    }
    return b;
}
int main(int argc, char **argv) {
 
    long int n;
 
    scanf("%ld", &n);
    printf("%ld", fib(n));
        return 0;
 
}
Reference: http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_thm_1.19

Thursday, May 27, 2010

Cogito ergo sum

"Cogito ergo sum( I think, therefore I am) is a philosophical statement in Latin by Rene Descartes, which became a fundamental element of Western Philosophy. The simple meaning of the phrase is that if someone is wondering whether or not they exist, that is in and of itself proof that they do exist (because, at the very least, there is an "I" who is doing the thinking) - Source: Wikipedia

Saturday, April 24, 2010

All of us are lying in the gutter...the difference is some of us are looking at the stars.-Oscar Wilde
Daylight at thousand stars hotel:

http://www.outlookindia.com/article.aspx?265171

agitprop - Political propaganda (especially Communist propaganda) communicated via art and literature and cinema

prosody - The patterns of stress and intonation in a language, (prosody) a system of versification, The study of poetic meter and the art of versification.

Tuesday, April 20, 2010

Didn't feel like going to work today. Watched "The Big Bang Theory" all the day. :)

Thursday, April 15, 2010

Underneath anger is hurt. Underneath hurt is love.

Thursday, April 1, 2010

Take chances, make mistakes. That's how you grow. Pain nourishes your courage. You have to fail in order to practice being brave.