Given a positive integer that 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.
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int A = 4;
if (A == 1)
cout << 1;
else if (A < 4)
cout << 0;
int N;
for (int P = 2; 2 <= pow(A + 1, 1 / P); P++) {
N = pow(A, 1 / P);
if (pow(N, P) == A)
cout << 1;
}
return 0;
}
New contributor
Shreya Shree is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.