- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Count ways to express a number as sum of powers in C++

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**

## Input

num=30

## Output

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

## Explanation

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}

## Input

num=35

## Output

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

## Explanation

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.

## Example

#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; }

## Output

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 Articles
- 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 18 as the sum of two prime numbers in all possible ways.
- 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++
- Express an odd number as sum of prime numbers in C++
- Express $17^2$ as the sum of two consecutive number.
- Express the sum of 9 as an odd consecutive number.
- Count number of ways to divide a number in parts in C++
- Program to check whether number is a sum of powers of three in Python
- Count the number of ways to traverse a Matrix in C++
- Count number of ways to reach a given score in a game
- 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++