Created
October 13, 2015 10:24
-
-
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
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
| <?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