Just to make sure I understood your code that if means that in case the k
is an odd number just multiply the accumulator 1 time with the val and
continue with even k.A question I have is if a recursive implementation
of this would be any faster?
Τη Σάββατο, 19 Ιανουαρίου 2013 1:06:25 μ.μ. UTC+2, ο χρήστης Guneesh έγραψε:
>
> consider code to find n^k where n is an integer
>
> int power()
> {
> int ans=1,val=1;
> while(k)
> {
> val=val*n;
> if(k&1)ans=ans*val;
> k=k>>1;
> }
> return ans;
> }
>
> now if n is is a matrix all you have to do is replace * by matrix
> multiplication algorithm
>
--