Last active
December 21, 2015 12:08
-
-
Save aaani/6303339 to your computer and use it in GitHub Desktop.
PHP implementation for 'Exponentiation by Squaring' method for finding power of a given number. This algorithm runs in O(log(P)) time where P is the power.
This file contains 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
function findPower($base, $exponent){ | |
if($exponent==0) return 1; | |
$output=$base; | |
$p=1; | |
while($p*2<$exponent){ | |
$output*=$output; | |
$p+=$p; | |
} | |
return $output*findPower($base, $exponent-$p); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment