- 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

In this tutorial, we are going to write a program that find the number of ways a integer can be expressed as sum of given n-th power of unique numbers.

We have two integers **number** and **power**. And we need to find in how many ways can we represent the given **number** as sum of n-th power of unique natural numbers. Let's see an example.

**Input** − number = 50, power = 2

**Output** − 3

There is only possible way we can write 4 as sum of 2 powers.

We will be using recursion to solve the problem. Let's see the steps to solve the problem.

Initialize the number and power.

Write a recursive function with a suitable name. It accepts

**number**,**power**and**i**as arguments.If the

**number**is less than zero or**pow(i, power)**is greater than**number**, then return 0.If the number is zero or pow(i, power) is equal to the number, then return 1.

We have two recursive calls for the functions to compute the total number of ways

Increment the

**i**.In the first recursive call, check for the numbers less than given number.

In the second recursive call, check for the given number.

Let's see the code.

#include <bits/stdc++.h> using namespace std; int findPossibleWaysCount(int number, int power, int i = 1) { if(number < 0 || number < pow(i, power)) { return 0; } if(number == 0 || number == pow(i, power)) { return 1; } return findPossibleWaysCount(number - pow(i, power), power, i + 1) + findPossibleWaysCount(number, power, i + 1); } int main() { // initializing the number and power int number = 50, power = 2; cout << findPossibleWaysCount(number, power) << endl; return 0; }

If you run the above code, then you will get the following result.

3

If you have any queries in the tutorial, mention them in the comment section.

- Related Questions & Answers
- C++ program to find ways an integer can be expressed as sum of n-th power of unique natural numbers
- C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2
- Check if an integer can be expressed as a sum of two semi-primes in Python
- Find m-th summation of first n natural numbers in C++
- Check if a number can be expressed as a sum of consecutive numbers in C++
- Check if a number can be expressed as power in C++
- Sum of sum of first n natural numbers in C++
- C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Check if a number can be expressed as sum two abundant numbers in C++
- Program to find sum of first n natural numbers in C++
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- Sum of first n natural numbers in C Program
- Find if n can be written as product of k numbers in C++
- Program to find number of ways we can get a number which is sum of nth power of unique numbers in Python
- C program for a number to be expressed as a sum of two prime numbers.

Advertisements