variksla
BAN USER
Comments (8)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
-1
of 1 vote
power by squaring:
pow(x, exp) = {
if x is even (x^2)(x^(2/n))
if x is odd x((x^2)^(n-1/2)
}
base cases:
if exp == 0 then return 1
if exp == 1 then return x itself
float powe(float x, int exp)
{
printf("%f %d\n", x, exp);
if (exp < 0)
return powe(1/x, -exp);
if (exp == 0)
return 1;
if (exp == 1)
return x;
if (((int)exp)%2==0)
return powe(x*x, exp/2);
else
return x*powe(x*x, (exp-1)/2);
}
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Nice logic.
- variksla August 03, 2015