mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-16 22:58:14 +01:00
26 lines
596 B
PHP
26 lines
596 B
PHP
<?php
|
|
|
|
/**
|
|
* This functions computes an exponent by doing repeated squares
|
|
*
|
|
* @param Integer $base the base
|
|
* @param Integer $exponent the exponent (ie the number of times that the base is multiplied by itself)
|
|
* @return Integer
|
|
*/
|
|
function fastExponentiation(int $base, int $exponent)
|
|
{
|
|
if ($exponent == 0) {
|
|
return 1;
|
|
}
|
|
|
|
if ($exponent == 1) {
|
|
return $base;
|
|
}
|
|
|
|
if ($exponent % 2 == 0) {
|
|
return fastExponentiation($base * $base, $exponent / 2);
|
|
} else {
|
|
return $base * fastExponentiation($base * $base, ($exponent - 1) / 2);
|
|
}
|
|
}
|