Ways to sum to N using Natural Numbers up to K with repetitions allowed | Function to find the total number of ways to represent N as the sum of integers over the range [ 1 , K ] ; Initialize a list ; Update dp [ 0 ] to 1 ; Iterate over the range [ 1 , K + 1 ] ; Iterate over the range [ 1 , N + 1 ] ; If col is greater than or equal to row ; Update current dp [ col ] state ; Return the total number of ways ; Given inputs
def NumberOfways ( N , K ) : NEW_LINE INDENT dp = [ 0 ] * ( N + 1 ) NEW_LINE dp [ 0 ] = 1 NEW_LINE for row in range ( 1 , K + 1 ) : NEW_LINE INDENT for col in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( col >= row ) : NEW_LINE INDENT dp [ col ] = dp [ col ] + dp [ col - row ] NEW_LINE DEDENT DEDENT DEDENT return ( dp [ N ] ) NEW_LINE DEDENT N = 8 NEW_LINE K = 2 NEW_LINE print ( NumberOfways ( N , K ) ) NEW_LINE