Integer raise-to-power-of
Don't cast back and forth to double just to use Math.Pow(). Seriously.
public static int Exp(this int x, int y)
{
int result = 1;
while (y > 0)
{
if ((y & 1) != 0)
{
result *= x;
}
y >>= 1;
x *= x;
}
return result;
}
Written by Alistair Young
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Math
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#