C++ Solution using Newton Raphson method


#1
int newtonRaph(int A,int p,int &ans){
double ep=0.1;
double g=A-1;
while(abs(pow(g,p)-A)>0.1){
    g=g-((pow(g,p)-A)/(p*(pow(g,p-1))));
}
ans=g;
if(pow(ans,p)!=A)
return 0;
return 1;

}
int Solution::isPower(int A) {
if(A==1)
return 1;

int p=2;int ans=A;
while(ans>=2){
    if(newtonRaph(A,p++,ans)){
        return 1;
    }
}
return 0;

}