Home » Php » math – How do I program something to get the number of possible outcomes in PHP?

math – How do I program something to get the number of possible outcomes in PHP?

Posted by: admin February 25, 2020 Leave a comment

Questions:

There are 3 boxes: boxA, boxB and boxC

You can put either 1 or 0 in each box

The possible outcomes are 1,1,1 1,1,0 1,0,0 0,0,0 0,0,1 0,1,1 0,1,0 1,0,1

So the answer is 8.

How do I code this if I could edit the number of variables? Like the number of boxes or the number of inputs I could do, not just 1 or 0.

Is it close to combination with repetition? I am not sure.

How to&Answers:
$possibilities = $number_of_variables ** $number_of_boxes

Answer:

It’s a simple answer that will give you all possible outcomes, which is 2 to the power of 3 (2**3), so 2 (all the possible values you can assign) to the power of 3 (all the items you have to assign those values, the boxes).

So 2**3 = 2*2*2 = 8