def count_partitions(n, m): if (n == 0 ): return 1 # there is only 1 way i.e. to leave it unpartitioned if (m == 0 or n < 0): return 0 # there is no way to divide n into 0 partions or if n is -ve (latter one from edge case of recursion call of n - m) else: return count_partitions(n - m, m) + count_partitions(n, m - 1) print(count_partitions(9, 5)) # 23