Dano
Dano

Reputation: 55

PHP script that calculates powers that are < X

I have a problem with my PHP script for calculating powers.

$A = $_POST["a"]; //Base number
$B = $_POST["b"]; //Value that POW of $A can't exceed. for example if A = 2, B = 1000, then highest solution = 512 

for ($i = 1; $A <= $B; $i++) {
            echo $i . ". ";
            echo pow($A, $i);
            echo '<br>';
        }

With the example i mentioned above, i know this script would still display 1024 but the problem is it doesn't work at all. I tried doing this many other ways and nothing works for me... please any guidance is really appreciated.

Upvotes: 0

Views: 45

Answers (2)

Dano
Dano

Reputation: 55

$C = 1;
for ($i = 1; $C <= $B; $i++) {
            $C = pow($A, $i);
            if ($C <= $B) {
                echo $i . ". " . $C . '<br>';
            } else {
                break;
            }
        }

Just added a variable $C that changes each time the loop runs so it can actually finish.

Upvotes: 0

wawa
wawa

Reputation: 5066

Let's take the example of $A = 2; $B = 10; so you'd expect to get 2, 4, 8.

Your for loop has as condition, execute as long as $A <= $B.

Let us manually do some iterations:
Run 1: $A: 2, $B: 10, ($A <= $B): true, $i: 1, output: "1. 2<br>"
Run 2: $A: 2, $B: 10, ($A <= $B): true, $i: 2, output: "1. 4<br>"
Run 3: $A: 2, $B: 10, ($A <= $B): true, $i: 3, output: "1. 8<br>"
Run 4: $A: 2, $B: 10, ($A <= $B): true, $i: 4, output: "1. 16<br>"
Run 5: $A: 2, $B: 10, ($A <= $B): true, $i: 5, output: "1. 32<br>"
Run X: $A: 2, $B: 10, ($A <= $B): true, $i: X, output: "1. 2^X<br>"

As we can see, the tested condition $A <= $B is always true. How to fix this? One option is to update variable $A, like this:

$A = $_POST["a"]; //Base number
$B = $_POST["b"]; //Value that POW of $A can't exceed. for example if A = 2, B = 1000, then highest solution = 512 

for ($i = 1; $A <= $B; $i++) {
    $A = pow($A, $i);
    if ($A <= $B) {
        echo $i . ". ";
        echo $A;
        echo '<br>';
    }
}

Another option would be to use a break condition:

$A = $_POST["a"]; //Base number
$B = $_POST["b"]; //Value that POW of $A can't exceed. for example if A = 2, B = 1000, then highest solution = 512 

for ($i = 1; true; $i++) {
    $A = pow($A, $i);
    if ($A > $B) {
        break;
    }
    echo $i . ". ";
    echo $A;
    echo '<br>';
}

This is an infinite loop, which usually gets written as

$A = $_POST["a"]; //Base number
$B = $_POST["b"]; //Value that POW of $A can't exceed. for example if A = 2, B = 1000, then highest solution = 512 

$i = 1;
while (true) {
    if ($A > $B) {
        break;
    }
    echo $i . ". ";
    $A = pow($A, $i);
    echo $A;
    echo '<br>';
    $i++;
}

One last thing, since $A and $B get submitted by a client, you shouldn't assume they're numbers. You can simply cast them to numbers using:

$A = (int) $_POST['a'];
$B = (int) $_POST['b'];

How I personally would write that code:

// get variables
$base = (int) $_POST["a"];
$max = (int) $_POST["b"];

// validate input
if ($max < $base) {
    echo 'error: Max number bigger than base number!<br>';
} elseif ($base < 2) {
    echo 'error: Base number needs to be >= 2!<br>';
}

/** SOLUTION 1 - using `pow` */
// starting exponent
$exp = 1;
// assign $base^$exp to $result and test it against $max
while (($result = pow($base, $exp)) <= $max) {
    // echo current row
    echo $exp.'. '.$result.'<br>';
    // increment exponent
    $exp++;
}

/** SOLUTION 2 - using dynamic programming instead of calling `pow` in each iteration */
// multiply $result with the base and check against $max
for ($result = 1, $exp = 1; ($result *= $base) <= $max; $exp++) {
    echo $exp.'. '.$result.'<br>';
}

/** In case you don't need the exponent:
for ($result = 1; ($result *= $base) <= $max;) {
    echo $result.'<br>';
}
*/

Try to pick variables that explain what they're doing and add comments to explain what's happening.

Upvotes: 1

Related Questions