Number of ways to get a given sum with n number of m | Javascript 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
const mod = 1000000007 ; let dp = new Array ( 55 ) ; for ( let i = 0 ; i < 55 ; i ++ ) dp [ i ] = new Array ( 55 ) . fill ( - 1 ) ; function NoofWays ( face , throws , sum ) { if ( sum == 0 && throws == 0 ) return 1 ; if ( sum < 0 throws == 0 ) return 0 ; if ( dp [ throws ] [ sum ] != - 1 ) return dp [ throws ] [ sum ] ; let ans = 0 ; for ( let i = 1 ; i <= face ; i ++ ) { ans += NoofWays ( face , throws - 1 , sum - i ) ; } return dp [ throws ] [ sum ] = ans ; } let faces = 6 , throws = 3 , sum = 12 ; document . write ( NoofWays ( faces , throws , sum ) ) ;