Dice Throw Problem
This algorithm finds the number of ways to get summation of values on each face when all the dice are thrown.
function FindWays($faces, $dice, $sum)
{
$table = array();
Fill2DArray($table, $dice + 1, $sum + 1, 0);
for ($i = 1; $i <= $faces && $i <= $sum; ++$i)
$table[1][$i] = 1;
for ($i = 2; $i <= $dice; ++$i)
{
for ($j = 1; $j <= $sum; ++$j)
{
for ($k = 1; $k <= $faces && $k < $j; ++$k)
{
$table[$i][$j] += $table[$i - 1][$j - $k];
}
}
}
return $table[$dice][$sum];
}
function Fill2DArray(&$arr, $rowCount, $colCount, $value)
{
for ($i = 0; $i < $rowCount; ++$i)
for ($j = 0; $j < $colCount; ++$j)
$arr[$i][$j] = $value;
}
Example
$result = FindWays(4, 3, 5);
Output
6