### Interview: Efficiently implement x^y for positive integers using basic operators.

There are three solutions that come to mind..

```public static int PowerOfY_0(int x, int y)
{
int result = 1;
for (int i = 0; i < y; i++)
result *= x;
return result;
}```

The second solution is a bit of fun, because you are not using the power function but other math functions. Definitely efficient in terms of lines of code.

```public static int PowerOfY_1(int x, int y)
{
return (int) Math.Round( Math.Exp(y* Math.Log(x, Math.E)));
}```

The expected function is something like this:

```public static int PowerOfY(int x, int y)
{
int result = 1;
while (y > 0)
{
if (y % 2 == 1)
result *= x;
y = y / 2;
x *= x;
}
return result;
}```
Of course, this is a bad question – you are really testing if the person can remember prior canned solutions and not their ability to think.  IMHO, the interviewer asking the question is suspect on their ability to think. It has no relationship to any job function.

1. Hi Ken,