Count ways to express a number as sum of powers Last Updated : 22 Nov, 2023 Comments Improve Suggest changes Like Article Like Report 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; } 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 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. 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. JavaScript <script> // JavaScript 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 let val = (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. function countWays(x, n) { return countWaysUtil(x, n, 1); } // Driver code let x = 100, n = 2; document.write(countWays(x, n)); // This code is contributed by Mayank Tyagi </script> 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. ?> Output: 3 Time Complexity: O(2 ^ pow(x^(1/n))) Auxiliary Space: O(pow(x^(1/n)) Comment More infoAdvertise with us Next Article Count ways to express a number as sum of powers A Aditya Prakash 5 Improve Article Tags : Misc Recursion DSA Practice Tags : MiscRecursion Similar Reads Count ways to express even number ânâ as sum of even integers Given an positive even integer 'n'. Count total number of ways to express ânâ as sum of even positive integers. Output the answer in modulo 109 + 7Examples: Input: 6 Output: 4 Explanation There are only four ways to write 6 as sum of even integers: 1. 2 + 2 + 2 2. 2 + 4 3. 4 + 2 4. 6 Input: 8 Output 7 min read Check if a number can be expressed as sum of two Perfect powers Given a positive integer n, the task is to check whether n can be expressed in the form of ax + by where x and y > 1 and a and b >= 0. If n can be expressed in the given form then print "Yes", otherwise print "No".Examples:Input: n = 5Output: YesExplanation: 5 can be expressed as 22 + 12 Input 5 min read Check if a number N can be expressed as the sum of powers of X or not Given two positive numbers N and X, the task is to check if the given number N can be expressed as the sum of distinct powers of X. If found to be true, then print "Yes", Otherwise, print "No". Examples: Input: N = 10, X = 3Output: YesExplanation:The given value of N(= 10) can be written as (1 + 9) 5 min read Count ways to represent an integer as an exponent Given an integer N, the task is to count the number of ways in which N can be expressed as an exponent, i.e., xy, where x and y are positive integers. Examples: Input: N = 64Output: 4Explanation: 64 can be expressed as 26, 43, 82 and 641 Input: N = 27Output: 2 Approach: The idea to solve the given p 10 min read Sum of digits of a given number to a given power Given a number, we need to find the sum of all the digits of a number which we get after raising the number to a specified power.Examples: Input: number = 5, power = 4 Output: 13 Explanation: Raising 5 to the power 4 we get 625. Now adding all the digits = 6 + 2 + 5 Input: number = 9, power = 5 Outp 3 min read Like