MrPizzaFace
MrPizzaFace

Reputation: 8086

Exponents without using the pow() function

For example pow(3,3) returns 27

I tried while and I tried a for loop. I'm missing something obvious. Just unsure what it is. Can someone walk me through this, please?

$i = 1; 
while ($i <= $exponent) {
    $result = ($base * $base);
    $result = $result * $result;
    $i++;   
    echo $result;
}

Upvotes: 1

Views: 6768

Answers (8)

Raj Kumar More
Raj Kumar More

Reputation: 89

Exponents without using the pow() function and Multiplication(*) factor. Not applicable if power is float.

echo userPower(5, -2); //0.04
echo userPower(5, 2); //125

function userPower(int $base, int $exponent)
{
    if($exponent ==0){
        return 1;
    }
    $pow =  $exponent < 0 ? abs($exponent) : $exponent;
    $x = $y = $base;
    for ($i = 1; $i < $pow; $i++) {
        for ($j = 1; $j < $base; $j++) {
            $x += $y;
        }
        $y = $x;
    }
    return $exponent < 0 ? 1 / $y : $y;
}

Upvotes: 0

akshay parmar
akshay parmar

Reputation: 59

function power($a,$b)
{
    if ($b==0) return 1;
    if ($b==1) return $a;
    if ($b%2==0) return power ($a*$a,$b/2);
    return $a*power($a*$a,($b-1)/2);
}

$total= power(2,16);'

may be above code needful someone..

Upvotes: 0

Sergey Onishchenko
Sergey Onishchenko

Reputation: 7851

This function should also be aware of possible negative exponent

function pow($num, $x)
{
    if ($x == 0) return 1;
    else if ($x > 0) return $num * pow($num, --$x);
    else return 1 / $num * pow($num, ++$x);
}

pow(2, 3) is 8
pow(2, -3) is 0.125

Upvotes: 0

$x = 3; // 3*3*3*3*3  => 5 time 
$n = 5;
$res = 1;
for ($i = 0; $i < $n; $i++)//i need 5time repeat
    {
    $res *= $x;
        //in here important
        // first time $res = 1 and $x = 3 ---> $res * $x --> 1*3 = 3
        //secend time $res = 3 and $x = 3 ---> $res * $x --> 3*3 = 9
        //next        $res = 9 and $x = 3 ---> $res * $x --> 9*3 = 27
        //next       $res = 27 and $x = 3 ---> $res * $x -> 27*3 = 81
        //final time $res = 81 and $x = 3 ---> $res * $x -> 81*3 = 243
    }
echo $res;
///// Output : 243

Upvotes: 0

andand
andand

Reputation: 17487

There is an identity you can use.

xy=ey×ln(x) 

Not sure how to translate to php.

Upvotes: 0

Vikram Bhat
Vikram Bhat

Reputation: 6246

Here is recursive pseudo code for very efficient pow function : -

  pow(a,b) {

    if(b==0) return 1

    temp = pow(a,b/2)

    if(b%2==0)
      return(temp*temp)

    else return(a*temp*temp)

  }

The above code is more intuitive than for loop & has time complexity of O(logb)

I am not familiar with php syntax.

Upvotes: 2

Floris
Floris

Reputation: 46375

You want to multiply a number by the base exponent times - so something like this:

function myPow($base, $exponent) {
  $result = 1;
  for($ii = 0; $ii < $exponent; $ii++) {
    $result *= $base;
  }
  return $result;
}

edited - just noticed the php tag...

Upvotes: 0

Ry-
Ry-

Reputation: 224942

I don’t really know how to describe what’s wrong, here – your code doesn’t make sense for calculating an exponent. Start with 1, multiply by the base $exponent times.

$result = 1;

for ($i = 0; $i < $exponent; $i++) {
    $result *= $base;
}

echo $result;

Upvotes: 2

Related Questions