
- 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
C++ program to find ways an integer can be expressed as sum of n-th power of unique natural numbers
In this article, we will be discussing a program to find ways an integer (say X) can be expressed as sum of n-th power of unique natural numbers.
For example, let X = 100 and n = 2
Then there would be 3 ways to express 100 as sum of squares of natural numbers.
100 = 102 100 = 62 + 82 100 = 12 + 32 + 42 + 52 + 72
This can be done easily by using recursion. We would start from 1 and go till the n-th root of the given number. In every run, we would subtract the n-th power of natural numbers (starting from 1) from the given number till the number become less than. This would give us the ways the number can be represented in as sum of n-th powers of natural numbers.
Example
#include<iostream> #include <math.h> using namespace std; int result = 0; int ways(int number, int a, int init, int n){ if (a == 0) { result++; } //setting the higher limit int max = (int)floor(pow(number, 1.0 / n)); for (int i = init + 1; i <= max; i++) { //subtracting n-th power values starting from 1 int b = a - (int)pow(i, n); if (b >= 0) ways(number, a - (int)pow(i, n), i, n); } return result; } int main() { int a = 100, n = 2; cout << ways(a, a, 0, n); return 0; }
Output
3
- Related Articles
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers in C++
- C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2
- C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Program to find sum of first n natural numbers in C++
- Check if an integer can be expressed as a sum of two semi-primes in Python
- Program to find number of ways we can get a number which is sum of nth power of unique numbers in Python
- Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Haskell Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Swift program to check whether a number can be expressed as sum of two prime numbers
- C program for a number to be expressed as a sum of two prime numbers.
- Check if a number can be expressed as a sum of consecutive numbers in C++
- Java program to find the sum of n natural numbers
- Find m-th summation of first n natural numbers in C++
- Sum of first n natural numbers in C Program
- 8085 program to find the sum of first n natural numbers

Advertisements