Skip to content

Instantly share code, notes, and snippets.

@giannispan
Created February 2, 2016 09:01
Show Gist options
  • Save giannispan/7f7fb568631b1b36157b to your computer and use it in GitHub Desktop.
Save giannispan/7f7fb568631b1b36157b to your computer and use it in GitHub Desktop.
Your task is to check wheter a given integer is a perfect power. If it is a perfect power, return a pair m and k with mk = n as a proof. Otherwise return Nothing, Nil, null, None or your language's equivalent.
var isPP = function(n){
var result=[];
for (var m=2; m<=Math.sqrt(n); m++)
{
var k = 2;
var p = Math.pow(m, k);
while (p<=n && p>0)
{
if (p==n)
result.push(m,k);
k++;
p = Math.pow(m, k);
}
}
while (result.length > 0) {
return result;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment