简体   繁体   English

如何使用PHP创建自己的pow功能?

[英]How I can create my own pow function using PHP?

I want to create a function in which I put two values (value and its power - Example function: multiply(3, 3) result 27 ). 我想创建一个函数,其中我放置两个值(值及其功率 - 示例函数: multiply(3, 3)结果27 )。 I have tried so far but failed, I have searched using Google but I have been unable to find any result because I don't know the name of this function. 到目前为止我已经尝试但失败了,我使用谷歌进行了搜索,但我找不到任何结果,因为我不知道这个函数的名称。

What I want exactly: 我想要的完全是:

3,3 => 3 x 3 x 3 = 27 3,3 => 3 x 3 x 3 = 27
4,4 => 4 x 4 x 4 x 4 = 256 4,4 => 4×4×4×4 = 256

What I tried: 我尝试了什么:

function multiply($value,$power){
    for($x = 1; $x <= $value; $x++ ){
        return $c = $value * $power;
    }   
}
echo multiply(3,3);

The answer has already been accepted, but I had to come here and say that all answers here use a bad algorithm. 答案已被接受,但我不得不来这里说这里的所有答案都使用了一个糟糕的算法。 There are better ones. 有更好的。 Including very simple ones, like exponentiation by squaring that reduces the complexity from O(power) to O(log(power)). 包括非常简单的那些,例如通过平方取幂 ,将复杂度从O(功率)降低到O(log(功率))。

The idea is to square the base while dividing the exponent by 2. For example 这个想法是在将指数除以2的同时对基数进行平方。例如

3^8 = 9^4 = 81^2 = 6561

There is a special case when the exponent is odd. 指数为奇数时有一种特殊情况。 In this case, you must store a separate variable to represent this factor: 在这种情况下,您必须存储一个单独的变量来表示此因子:

2^10 = 4^5 = 16^2 * 4 = 256 * 4 = 1024

PHP isn't one of my strong skills, but the final algorithm is as simple as: PHP不是我强大的技能之一,但最终的算法很简单:

function multiply($value, $power){
    $free = 1;
    while ($power > 1) {
        if ($power % 2 == 1)
            $free *= $value;
        $value *= $value;
        $power >>= 1; //integer divison by 2
    }
    return $value*$free;
}
echo multiply(3, 3) . "\n";
echo multiply(2, 10) . "\n";
echo multiply(3, 8) . "\n";

Oopsika, couldn't have asked a more obvious question. Oopsika,不可能问一个更明显的问题。 Use the built-in function named pow (as in a lot of languages) 使用名为pow的内置函数(如许多语言)

echo pow(3, 3);

Edit 编辑

Let's create our own function. 让我们创建自己的功能。

function raiseToPower($base,$exponent)
{
    // multiply the base to itself exponent number of times
    $result=1;
    for($i=1;$i<=$exponent;$i++)
    {
      $result = $result * $base;  
    }
    return $result;
}
function exponent($value,$power)
{
    $c=1; 
    for($x = 1; $x <= $power; $x++ )
    {
        $c = $value * $c;
    } 
return $c;    
}
  • If you have PHP >= 5.6 you can use the ** operator 如果你有PHP> = 5.6,你可以使用**运算符

$a ** $b Exponentiation Result of raising $a to the $b'th power. $ a ** $ b Exponentiation将$ a筹集到$ b'th权力的结果。

echo 2 ** 3;
  • If you have PHP < 5.6 you can use pow: 如果你有PHP <5.6你可以使用pow:

number pow ( number $base , number $exp ) 数字pow(数字$ base,数字$ exp)

echo pow(2, 3);
  • Your own function is: 你自己的功能是:

function multiply($value, $power) {

    $result = 1;

    for($x = 1; $x <= $power; $x++){
        $result *= $value;
    }   

    return $result;
}

echo multiply(3,3);

Read more at: 阅读更多:

http://php.net/manual/en/language.operators.arithmetic.php http://php.net/manual/en/language.operators.arithmetic.php

http://php.net/manual/en/function.pow.php http://php.net/manual/en/function.pow.php

Just try to run this code I hope your problem will be solved. 只是尝试运行此代码我希望您的问题将得到解决。 If you defining any function then you have to call it return value. 如果定义任何函数,则必须将其称为返回值。

<?php
    function multiply($value,$exp)
    {    $temp=1;
        if($exp==0)
            return $temp;
        else
        {
             for($i=1;$i<=$exp;$i++)
             $temp=$temp*$value;
             return $temp;
        }

    }

    echo multiply(5,6);

    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM