Skip to content

Instantly share code, notes, and snippets.

@cruxrebels
Created April 27, 2016 19:12
Show Gist options
  • Save cruxrebels/9b7376fe663f1f5ce2c03458eecf96b5 to your computer and use it in GitHub Desktop.
Save cruxrebels/9b7376fe663f1f5ce2c03458eecf96b5 to your computer and use it in GitHub Desktop.
Given a positive integer which fits in a 32 bit signed integer, find if it can be expressed as A^P where P > 1 and A > 0. A and P both should be integers. Example Input : 4 Output : True as 2^2 = 4. Tags: InterviewBit Math Problem https://www.interviewbit.com/problems/power-of-two-integers/
bool Solution::isPower(int A) {
if (A<2)
return true;
for (auto i = 2; i<=sqrt(A); ++i)
{
for (auto j = 2; j<=32; ++j)
{
if(pow(i, j)==A)
return true;
}
}
return false;
}
@110CodingP
Copy link

Thanks, this solution is very helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment