Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
Recursion:
Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.
Find the number of ways a given integer can be represented as the sum of the Nth power of the unique, natural integers using this program. For example, if X = 100 and N = 2, we must identify all square combinations that add up to 100. The potential solutions are (10,2), (8^2+6^2), (1^2+3^2+4^2+5^2+7^2). As a result, the total number of solutions is three.
Examples:
Example1:
Input:
Given number = 25 Given N =2
Output:
The possible solutions count = 2
Example2:
Input:
Given number = 100 Given N =2
Output:
The possible solutions count = 3
Python Program to Calculate Sum of Nth Power using Recursion
Below are the ways to write a program that calculates the sum of Nth Power using Recursion.
Method #1: Using Recursion (Static Input)
Approach:
- Give the number and N as static input and store them in two separate variables.
- Create a function countNPower() function which returns the total number of possible solutions count.
- Pass the given number and N,1 as arguments to the countNPower() function.
- Check to see if the number equals 1 power N, if so, there is only one potential/possible solution.
- There is no solution if the number is smaller than 1 power of N.
- If number is more than 1 power N, then countNPower(value, N, num+1)+countNPower(number, N, num+1) is returned.
- The first countNPowercall includes the 1 power N value, but the second one excludes it.
- Print the Result.
- The Exit of the Program.
Below is the implementation:
# Create a function countNPower() function which # returns the total number of possible solutions count. def countNPower(Number, n, numb): resultval = Number-pow(numb, n) # There is no solution if the number is smaller than 1 power of N. if resultval < 0: return 0 # Check to see if the number equals 1 power N, # if so, there is only one potential/possible solution. elif resultval == 0: return 1 # If number is more than 1 power N, then # countNPower(value, N, num+1)+countNPower(number, N, num+1) is returned. # The first countNPowercall includes the 1 power N value, # but the second one excludes it. else: return countNPower(resultval, n, numb+1)+countNPower(Number, n, numb+1) # Give the number and N as static input and store them in two separate variables. Numb = 25 n = 2 # Pass the given number and N,1 as arguments to the countNPower() function. print('The possible solutions count = ', countNPower(Numb, n, 1))
Output:
The possible solutions count = 2
Method #2: Using Recursion (User Input)
Approach:
- Give the number and N as user input using map(),split(), and input() functions.
- Store them in two separate variables.
- Create a function countNPower() function which returns the total number of possible solutions count.
- Pass the given number and N,1 as arguments to the countNPower() function.
- Check to see if the number equals 1 power N, if so, there is only one potential/possible solution.
- There is no solution if the number is smaller than 1 power of N.
- If number is more than 1 power N, then countNPower(value, N, num+1)+countNPower(number, N, num+1) is returned.
- The first countNPowercall includes the 1 power N value, but the second one excludes it.
- Print the Result.
- The Exit of the Program.
Below is the implementation:
# Create a function countNPower() function which # returns the total number of possible solutions count. def countNPower(Number, n, numb): resultval = Number-pow(numb, n) # There is no solution if the number is smaller than 1 power of N. if resultval < 0: return 0 # Check to see if the number equals 1 power N, # if so, there is only one potential/possible solution. elif resultval == 0: return 1 # If number is more than 1 power N, then # countNPower(value, N, num+1)+countNPower(number, N, num+1) is returned. # The first countNPowercall includes the 1 power N value, # but the second one excludes it. else: return countNPower(resultval, n, numb+1)+countNPower(Number, n, numb+1) # Give the number and N as user input using map(),split(), and input() functions. # Store them in two separate variables. Numb, n = map(int, input( 'Enter some random number and N separated by spaces = ').split()) # Pass the given number and N,1 as arguments to the countNPower() function. print('The possible solutions count = ', countNPower(Numb, n, 1))
Output:
Enter some random number and N separated by spaces = 100 2 The possible solutions count = 3
Related Programs:
- python program to find the power of a number using recursion
- python program to find the total sum of a nested list using recursion
- python program to find the product of two numbers using recursion
- python program to find the lcm of two numbers using recursion
- python program to find the length of a list using recursion
- python program to calculate the length of a string without using a library function
- python program to implement tower of hanoi using recursion