Calculate x ^ y in O(log n)
Calculate x ^ y in O(log n)

There are different answer like Use the Indian Power algorithm

or 

    double power(double x, int y) {
    if(y == 0) return 1;

    double d = power(x, y/2);

    if(y%2 == 0) return d*d;
    else return x*d*d;
    }


1. is it a correct answer?


2. is there any better way of writing a code for this question?


Thanks in advance!
Calculate x ^ y in O(log n)
===

Calculate x ^ y in O(log n)

There are different answer like Use the Indian Power algorithm

or 

    double power(double x, int y) {
    if(y == 0) return 1;

    double d = power(x, y/2);

    if(y%2 == 0) return d*d;
    else return x*d*d;
    }


1. is it a correct answer?


2. is there any better way of writing a code for this question?


Thanks in advance!