Created
April 27, 2016 19:12
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, this solution is very helpful!