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 Shared Function FindWays(faces As Integer, dice As Integer, sum As Integer) As Integer
	Dim table As Integer(,) = New Integer(dice, sum) {}

	Dim i As Integer = 1
	While i <= faces AndAlso i <= sum
		table(1, i) = 1
		i += 1
	End While

	For i = 2 To dice
		For j As Integer = 1 To sum
			Dim k As Integer = 1
			While k <= faces AndAlso k < j
				table(i, j) += table(i - 1, j - k)
				k += 1
			End While
		Next
	Next

	Return table(dice, sum)
End Function
								


Example

									Dim result As Integer = FindWays(4, 3, 5)
								


Output

									6