Number of ways to get a given sum with n number of m | Python3 function to calculate the number of ways to achieve sum x in n no of throws ; Function to calculate recursively the number of ways to get sum in given throws and [ 1. . m ] values ; Base condition 1 ; Base condition 2 ; If value already calculated dont move into re - computation ; Recursively moving for sum - i in throws - 1 no of throws left ; Inserting present values in dp ; Driver function
import numpy as np NEW_LINE mod = 1000000007 ; NEW_LINE dp = np . zeros ( ( 55 , 55 ) ) ; NEW_LINE def NoofWays ( face , throws , sum ) : NEW_LINE INDENT if ( sum == 0 and throws == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT if ( sum < 0 or throws == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( dp [ throws ] [ sum ] != - 1 ) : NEW_LINE INDENT return dp [ throws ] [ sum ] ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for i in range ( 1 , face + 1 ) : NEW_LINE INDENT ans += NoofWays ( face , throws - 1 , sum - i ) ; NEW_LINE DEDENT dp [ throws ] [ sum ] = ans ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT faces = 6 ; throws = 3 ; sum = 12 ; NEW_LINE for i in range ( 55 ) : NEW_LINE INDENT for j in range ( 55 ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT print ( NoofWays ( faces , throws , sum ) ) ; NEW_LINE DEDENT