Count ways to express a number as sum of powers

Given two integers x and n, we need to find number of ways to express x as sum of n-th powers of unique natural numbers. It is given that 1 <= n <= 20.

how to solve this problem using recursion

Hi Shubham, the above problem can be easily solved using recursion. Let num be given number, n power to be raised and i whose power will be raised and added to obtain num as sum, if possible. Its recurrence relation will be:

                            |   1, if num - i^n = 0
f ( num , n , i ) =   |   0, if num - i^n < 0
                            |   f(num-i^n, n, i+1) + f(num, n, i+1)

Hope this helps :slight_smile:

Hey shubham,
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.