- 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

Suppose we have a number x and another number n. We have to find number of ways we can get x as sum of nth power of some unique numbers.

So, if the input is like x = 100 n = 2, then the output will be 3 because possible solutions are 6^2 + 8^2, 10^2 and 1^2 + 3^2 + 4^2 + 5^2 + 7^2.

To solve this, we will follow these steps −

- ans := 0
- Define a method called solve() this will take four parameters x, n, cn and cs, the initial
- values for cs = 0, cn = 1
- p := cn^n
- while p + cs < x, do
- ans := ans + solve(x, n, cn + 1, p + cs)
- cn := cn + 1
- p := cn^n

- if p + cs is same as x, then
- ans := ans + 1

- return ans

Let us see the following implementation to get better understanding −

from math import pow def solve(x, n, cn = 1, cs = 0): ans = 0 p = pow(cn, n) while p + cs < x: ans += solve(x, n, cn + 1, p + cs) cn = cn + 1 p = pow(cn, n) if p + cs == x: ans = ans + 1 return ans x = 100 n = 2 print(solve(x, n))

100, 2

3

- Related Questions & Answers
- 8085 program to find nth power of a number
- Program to find number of ways we can decode a message in Python
- Program to find number of ways we can split a palindrome in python
- Program to find number of ways we can arrange symbols to get target in Python?
- C++ program to find ways an integer can be expressed as sum of n-th power of unique natural numbers
- Program to find number of ways we can get n R.s using Indian denominations in Python
- Program to find number of ways where square of number is equal to product of two numbers in Python
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers in C++
- Program to find maximum number of coins we can get using Python
- Program to find number of ways we can concatenate words to make palindromes in Python
- C++ Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
- Program to find number of ways we can select sequence from Ajob Sequence in Python
- Program to count number of ways we can throw n dices in Python
- Program to count number of ways we can distribute coins to workers in Python
- How to find power of a number in Python?

Advertisements