I'm trying to see if there is a good way to find whether, given int
s b
and n
, there exists an int a
such that a^n=b
. In other words, something more efficient than the bad solution I wrote below
private static bool HasBase(int b, int n)
{
for(int a = 1; a <= int.MaxValue; ++a)
{
int pow = Power(a, n);
if(pow == b)
return true;
else if(pow > b)
return false;
}
return false;
}
private static int Power(int a, int n)
{
return Enumerable.Range(a, n).Aggregate(1, (prev, cur) => prev * cur);
}