# 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.

Examples:

Input : x = 100 n = 2 Output : 3 Explanation: There are three ways to express 100 as sum of natural numbers raised to power 2. 100 = 10^2 = 8^2+6^2 = 1^2+3^2+4^2+5^2+7^2 Input : x = 100 n = 3 Output : 1 Explanation : The only combination is, 1^3 + 2^3 + 3^3 + 4^3

We use recursion to solve the problem. We first check one by one that the number is included in summation or not.

## C++

`// C++ program to count number of ways ` `// to express x as sum of n-th power ` `// of unique natural numbers. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// num is current num. ` `int` `countWaysUtil(` `int` `x, ` `int` `n, ` `int` `num) ` `{ ` ` ` `// Base cases ` ` ` `int` `val = (x - ` `pow` `(num, n)); ` ` ` `if` `(val == 0) ` ` ` `return` `1; ` ` ` `if` `(val < 0) ` ` ` `return` `0; ` ` ` ` ` `// Consider two possibilities, num is ` ` ` `// included and num is not included. ` ` ` `return` `countWaysUtil(val, n, num + 1) + ` ` ` `countWaysUtil(x, n, num + 1); ` `} ` ` ` `// Returns number of ways to express ` `// x as sum of n-th power of two. ` `int` `countWays(` `int` `x, ` `int` `n) ` `{ ` ` ` `return` `countWaysUtil(x, n, 1); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `x = 100, n = 2; ` ` ` `cout << countWays(x, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count number of ways ` `// to express x as sum of n-th power ` `// of unique natural numbers. ` `public` `class` `GFG { ` ` ` ` ` `// num is current num. ` ` ` `static` `int` `countWaysUtil(` `int` `x, ` `int` `n, ` `int` `num) ` ` ` `{ ` ` ` `// Base cases ` ` ` `int` `val = (` `int` `) (x - Math.pow(num, n)); ` ` ` `if` `(val == ` `0` `) ` ` ` `return` `1` `; ` ` ` `if` `(val < ` `0` `) ` ` ` `return` `0` `; ` ` ` ` ` `// Consider two possibilities, num is ` ` ` `// included and num is not included. ` ` ` `return` `countWaysUtil(val, n, num + ` `1` `) + ` ` ` `countWaysUtil(x, n, num + ` `1` `); ` ` ` `} ` ` ` ` ` `// Returns number of ways to express ` ` ` `// x as sum of n-th power of two. ` ` ` `static` `int` `countWays(` `int` `x, ` `int` `n) ` ` ` `{ ` ` ` `return` `countWaysUtil(x, n, ` `1` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `x = ` `100` `, n = ` `2` `; ` ` ` `System.out.println(countWays(x, n)); ` ` ` `} ` `} ` `// This code is contributed by Sumit Ghosh ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to count number of ways ` `# to express x as sum of n-th power ` `# of unique natural numbers. ` ` ` `# num is current num. ` `def` `countWaysUtil(x,n,num): ` ` ` ` ` `# Base cases ` ` ` `val ` `=` `(x ` `-` `pow` `(num, n)) ` ` ` `if` `(val ` `=` `=` `0` `): ` ` ` `return` `1` ` ` `if` `(val < ` `0` `): ` ` ` `return` `0` ` ` ` ` `# Consider two possibilities, num is ` ` ` `# included and num is not included. ` ` ` `return` `countWaysUtil(val, n, num ` `+` `1` `) ` `+` `\ ` ` ` `countWaysUtil(x, n, num ` `+` `1` `) ` ` ` ` ` `# Returns number of ways to express ` `# x as sum of n-th power of two. ` `def` `countWays(x,n): ` ` ` `return` `countWaysUtil(x, n, ` `1` `) ` ` ` ` ` `# Driver code ` `x ` `=` `100` `n ` `=` `2` ` ` `print` `(countWays(x, n)) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count number of ways ` `// to express x as sum of n-th power ` `// of unique natural numbers. ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// num is current num. ` ` ` `static` `int` `countWaysUtil(` `int` `x, ` ` ` `int` `n, ` `int` `num) ` ` ` `{ ` ` ` ` ` `// Base cases ` ` ` `int` `val = (` `int` `) (x - Math.Pow(num, n)); ` ` ` `if` `(val == 0) ` ` ` `return` `1; ` ` ` `if` `(val < 0) ` ` ` `return` `0; ` ` ` ` ` `// Consider two possibilities, ` ` ` `// num is included and num is ` ` ` `// not included. ` ` ` `return` `countWaysUtil(val, n, num + 1) ` ` ` `+ countWaysUtil(x, n, num + 1); ` ` ` `} ` ` ` ` ` `// Returns number of ways to express ` ` ` `// x as sum of n-th power of two. ` ` ` `static` `int` `countWays(` `int` `x, ` `int` `n) ` ` ` `{ ` ` ` `return` `countWaysUtil(x, n, 1); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `x = 100, n = 2; ` ` ` ` ` `Console.WriteLine(countWays(x, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count number of ways ` `// to express x as sum of n-th power ` `// of unique natural numbers. ` ` ` `// num is current num. ` `function` `countWaysUtil(` `$x` `, ` `$n` `, ` `$num` `) ` `{ ` ` ` ` ` `// Base cases ` ` ` `$val` `= (` `$x` `- pow(` `$num` `, ` `$n` `)); ` ` ` `if` `(` `$val` `== 0) ` ` ` `return` `1; ` ` ` `if` `(` `$val` `< 0) ` ` ` `return` `0; ` ` ` ` ` `// Consider two possibilities, num is ` ` ` `// included and num is not included. ` ` ` `return` `(countWaysUtil(` `$val` `, ` `$n` `, ` `$num` `+ 1) + ` ` ` `countWaysUtil(` `$x` `, ` `$n` `, ` `$num` `+ 1)); ` `} ` ` ` `// Returns number of ways to express ` `// x as sum of n-th power of two. ` `function` `countWays(` `$x` `, ` `$n` `) ` `{ ` ` ` `return` `countWaysUtil(` `$x` `, ` `$n` `, 1); ` `} ` ` ` `// Driver code ` `$x` `= 100; ` `$n` `= 2; ` `echo` `(countWays(` `$x` `, ` `$n` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output:

3

This article is contributed by **Anjali**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Program for Sum of the digits of a given number
- Write a program to reverse digits of a number
- Given an array A[] and a number x, check for pair in A[] with sum as x
- Check if a number is Palindrome
- Count all possible paths from top left to bottom right of a mXn matrix
- How to print maximum number of A's using given four keys
- Print all non-increasing sequences of sum equal to a given number x
- Count number of ways to partition a set into k subsets
- Happy Number
- Highest power of 2 less than or equal to given number
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Decode a string recursively encoded as count followed by substring
- Recursive solution to count substrings with same first and last characters
- Minimum tiles of sizes in powers of two to cover whole area
- Count subtrees that sum up to a given value x only using single recursive function