This is a blog for interview questions. The questions are primarily from certain web forums, BBS or books. The owner of this blog doesn't claim any copyright of those questions. The blog is established primarily to help the blog owner prepare job interviews.
Monday, June 27, 2011
Efficient Way to Calculate Power(x,y)
Problem: Give your own implementation of power(x,y), where y is a nature number.
Solution: There is a neat solution:
double power(double base, int exp)
{
int res = 1;
while(exp)
{
if(exp & 1) res *= base;
exp >>= 1;
base *= base;
}
return res;
}
No comments:
Post a Comment