- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given two numbers num and power as input. The goal is to find the ways in which num can be represented as a sum of unique natural numbers raised to the given power. If num is 10 and power is 2 then we can represent 10 as 12+32. So total 1 way.

**For Example**

num=30

Count of ways to express a number as sum of powers are: 2

The ways in which we can express 30 as sum of powers: 1^{2}+ 2^{2}+ 5^{2}and 1^{2}+ 2^{2}+ 3^{2}+ 4^{2}

num=35

Count of ways to express a number as sum of powers are: 1

The ways in which we can express ‘num’ as sum of powers: 2^{2}+ 3^{2}

**Approach used in the below program is as follows** −

In this approach we first check if the number is itself the power of any num^{power}. If yes then return ways as 1, if not then recursively check for sum num^{power} + (num+1)^{power}.

Take two integers num and power as input.

Function sum_of_powers(int num, int power, int val) takes a num and returns the count of ways to express ‘num’ as sum of unique natural numbers raised to the given power.

Take check=(num − pow(val, power)). If check is 0 then return 1 as the number itself is val

^{power}.If check is less than 0 then return 0.

Otherwise take temp=val+1.

Return sum of ( sum_of_powers(check, power, temp) + sum_of_powers(num, power, temp) ).

At the end we will get ways to return value.

#include <bits/stdc++.h> using namespace std; int sum_of_powers(int num, int power, int val){ int check = (num − pow(val, power)); if(check == 0){ return 1; } else if(check < 0){ return 0; } else { int temp = val + 1; return sum_of_powers(check, power, temp) + sum_of_powers(num, power, temp); } } int main(){ int num = 25, power = 2; cout<<"Count of ways to express a number as sum of powers are: "<<sum_of_powers(num, power, 1); return 0; }

If we run the above code it will generate the following output −

Count of ways to express a number as sum of powers are: 2

- Related Questions & Answers
- Count ways to express a number as sum of consecutive numbers in C++
- Count ways to express ‘n’ as sum of odd integers in C++
- Count of different ways to express N as the sum of 1, 3 and 4 in C++
- Express an odd number as sum of prime numbers in C++
- Minimum number of palindromes required to express N as a sum using C++.
- Check if a number can be represented as sum of non zero powers of 2 in C++
- Count number of ways to divide a number in parts in C++
- Powers of 2 to required sum in C++
- Count the number of ways to traverse a Matrix in C++
- Minimum numbers needed to express every integer below N as a sum in C++
- C++ Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
- Count number of ways to reach destination in a Maze in C++
- Count number of ways to jump to reach end in C++
- Count number of ways to reach a given score in a Matrix in C++
- Count number of ways to partition a set into k subsets in C++

Advertisements