Dice Throw Problem

This algorithm finds the number of ways to get summation of values on each face when all the dice are thrown.



									public static int FindWays(int faces, int dice, int sum)
{
	int[,] table = new int[dice + 1, sum + 1];

	for (int i = 1; i <= faces && i <= sum; ++i)
		table[1, i] = 1;

	for (int i = 2; i <= dice; ++i)
	{
		for (int j = 1; j <= sum; ++j)
		{
			for (int k = 1; k <= faces && k < j; ++k)
			{
				table[i, j] += table[i - 1, j - k];
			}
		}
	}

	return table[dice, sum];
}
								


Example

									int result = FindWays(4, 3, 5);
								


Output

									6