Skip to content

Instantly share code, notes, and snippets.

@ragingprodigy
Created October 13, 2015 10:24
Show Gist options
  • Select an option

  • Save ragingprodigy/3308a9863c05a3ec37dd to your computer and use it in GitHub Desktop.

Select an option

Save ragingprodigy/3308a9863c05a3ec37dd to your computer and use it in GitHub Desktop.
Determine if an integer Z can be expressed in the form P^Q using PHP
<?php
/**
* Return 1 if $Z can be expressed as on integer raised to the power of another integer
*/
function superPower( $Z) {
$half = (int) sqrt($Z);
for ($x=2; $x<$half; $x++) {
if ($Z % $x == 0) {
for ($y=1; $y<$half; $y++) {
if (pow($x,$y) == $Z) {
//echo "$x ^ $y = $Z";
return 1;
}
}
}
}
return 0;
}
echo superPower(25); // Should return 1
echo superPower(26); // Should return 0
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment