1

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;
}
Ry-
  • 209,133
  • 54
  • 439
  • 449
MrPizzaFace
  • 7,569
  • 15
  • 73
  • 120
  • Your implementation only supports positive integer exponents. What if I want `2` raised to `-1/2`? – Dai Nov 16 '13 at 04:17
  • It only needs to support positive whole numbers. – MrPizzaFace Nov 16 '13 at 04:17
  • 1
    @minitech I feel like "throwing" my computer out of my kitchen window because I am very "tired". Thanks for correcting that. I need sleep :/ – MrPizzaFace Nov 16 '13 at 04:22
  • possible duplicate of [How can I write a power function myself?](http://stackoverflow.com/questions/2882706/how-can-i-write-a-power-function-myself) – Jerry Coffin Nov 16 '13 at 04:54

7 Answers7

2

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;
Ry-
  • 209,133
  • 54
  • 439
  • 449
  • While it is good to tell to beginners but the above implementation is very inefficient because it takes O(exponent). – Vikram Bhat Nov 16 '13 at 04:35
2

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.

Vikram Bhat
  • 5,972
  • 3
  • 18
  • 19
0

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...

Floris
  • 45,045
  • 6
  • 64
  • 115
0

There is an identity you can use.

xy=ey×ln(x) 

Not sure how to translate to php.

andand
  • 16,364
  • 9
  • 51
  • 77
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
0

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

Sergey Onishchenko
  • 5,757
  • 4
  • 42
  • 46
0
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..