Search Algorithm n ^ p :
unsigned long long power(unsigned n, unsigned p) { unsigned long long x=1, y=n; while(p > 0) { if(p&1) x *= y; y *= y; p >>= 1; } return x; }
Can someone explain the logic / math behind this algorithm. I know this works, and designed it for several test cases (dry run). I mean how it works and how effective it is from a general naive method.
source share