聘我网

新概念招聘3.0

高效的指数计算实现

vote up0vote downstar

实现一个类似pow($base,$exp)的函数,采用Exponentiation by squaring,效率会大大提高。

原理:

alt text

实现:

function power($base,$exp)
{
    $result = 1;
    while($exp)
    {
        if($exp & 1)
            $result *= $base;
        $exp >>= 1;
        $base *= $base;
    }
    return $result;
}
 

1 个答复

vote up0vote down

Power(Power(x,n/2),2)

链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.