Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
Given two numbers x and n, find a number of ways x can be expressed as sum of n-th power of unique natural numbers.
Examples :
Input : x = 10, n = 2
Output : 1
Explanation: 10 = 12 + 32, Hence total 1 possibility
Input : x = 100, n = 2
Output : 3
Explanation:
100 = 102 OR 62 + 82 OR 12 + 32 + 42 + 52 + 72 Hence total 3 possibilities
The idea is simple. We iterate through all number starting from 1. For every number, we recursively try all greater numbers and if we are able to find sum, we increment result
C++
#include <bits/stdc++.h>
using namespace std;
int power( int num, unsigned int n)
{
if (n == 0)
return 1;
else if (n % 2 == 0)
return power(num, n / 2) * power(num, n / 2);
else
return num * power(num, n / 2) * power(num, n / 2);
}
int checkRecursive( int x, int n, int curr_num = 1,
int curr_sum = 0)
{
int results = 0;
int p = power(curr_num, n);
while (p + curr_sum < x) {
results += checkRecursive(x, n, curr_num + 1,
p + curr_sum);
curr_num++;
p = power(curr_num, n);
}
if (p + curr_sum == x)
results++;
return results;
}
int main()
{
int x = 10, n = 2;
cout << checkRecursive(x, n);
return 0;
}
|
Java
class GFG {
static int power( int num, int n)
{
if (n == 0 )
return 1 ;
else if (n % 2 == 0 )
return power(num, n / 2 ) * power(num, n / 2 );
else
return num * power(num, n / 2 )
* power(num, n / 2 );
}
static int checkRecursive( int x, int n, int curr_num,
int curr_sum)
{
int results = 0 ;
int p = power(curr_num, n);
while (p + curr_sum < x) {
results += checkRecursive(x, n, curr_num + 1 ,
p + curr_sum);
curr_num++;
p = power(curr_num, n);
}
if (p + curr_sum == x)
results++;
return results;
}
public static void main(String[] args)
{
int x = 10 , n = 2 ;
System.out.println(checkRecursive(x, n, 1 , 0 ));
}
}
|
Python3
def power(num, n):
if (n = = 0 ):
return 1
elif (n % 2 = = 0 ):
return power(num, n / / 2 ) * power(num, n / / 2 )
else :
return num * power(num, n / / 2 ) * power(num, n / / 2 )
def checkRecursive(x, n, curr_num = 1 , curr_sum = 0 ):
results = 0
p = power(curr_num, n)
while (p + curr_sum < x):
results + = checkRecursive(x, n, curr_num + 1 , p + curr_sum)
curr_num = curr_num + 1
p = power(curr_num, n)
if (p + curr_sum = = x):
results = results + 1
return results
if __name__ = = '__main__' :
x = 10
n = 2
print (checkRecursive(x, n))
|
C#
using System;
class GFG {
static int power( int num, int n)
{
if (n == 0)
return 1;
else if (n % 2 == 0)
return power(num, n / 2) * power(num, n / 2);
else
return num * power(num, n / 2)
* power(num, n / 2);
}
static int checkRecursive( int x, int n, int curr_num,
int curr_sum)
{
int results = 0;
int p = power(curr_num, n);
while (p + curr_sum < x) {
results += checkRecursive(x, n, curr_num + 1,
p + curr_sum);
curr_num++;
p = power(curr_num, n);
}
if (p + curr_sum == x)
results++;
return results;
}
public static void Main()
{
int x = 10, n = 2;
System.Console.WriteLine(
checkRecursive(x, n, 1, 0));
}
}
|
PHP
<?php
function power( $num , $n )
{
if ( $n == 0)
return 1;
else if ( $n % 2 == 0)
return power( $num , (int)( $n / 2)) *
power( $num , (int)( $n / 2));
else
return $num * power( $num , (int)( $n / 2)) *
power( $num , (int)( $n / 2));
}
function checkRecursive( $x , $n ,
$curr_num = 1,
$curr_sum = 0)
{
$results = 0;
$p = power( $curr_num , $n );
while ( $p + $curr_sum < $x )
{
$results += checkRecursive( $x , $n ,
$curr_num + 1,
$p + $curr_sum );
$curr_num ++;
$p = power( $curr_num , $n );
}
if ( $p + $curr_sum == $x )
$results ++;
return $results ;
}
$x = 10; $n = 2;
echo (checkRecursive( $x , $n ));
?>
|
Javascript
<script>
function power(num , n)
{
if (n == 0)
return 1;
else if (n % 2 == 0)
return power(num, parseInt(n / 2)) * power(num, parseInt(n / 2));
else
return num * power(num,parseInt(n / 2)) * power(num, parseInt(n / 2));
}
function checkRecursive(x , n , curr_num , curr_sum)
{
var results = 0;
var p = power(curr_num, n);
while (p + curr_sum < x)
{
results += checkRecursive(x, n, curr_num + 1, p + curr_sum);
curr_num++;
p = power(curr_num, n);
}
if (p + curr_sum == x)
results++;
return results;
}
var x = 10, n = 2;
document.write(checkRecursive(x, n, 1, 0));
</script>
|
Time Complexity: O(x^(1/n)), which is the maximum possible value of curr_num.
Space Complexity: O(log(x)) for the recursion stack.
Alternate Solution :
Below is an alternate simpler solution provided by Shivam Kanodia.
C++
#include<bits/stdc++.h>
using namespace std;
int res = 0;
int checkRecursive( int num, int x, int k, int n)
{
if (x == 0)
res++;
int r = ( int ) floor ( pow (num, 1.0 / n));
for ( int i = k + 1; i <= r; i++)
{
int a = x - ( int ) pow (i, n);
if (a >= 0)
checkRecursive(num, x -
( int ) pow (i, n), i, n);
}
return res;
}
int check( int x, int n)
{
return checkRecursive(x, x, 0, n);
}
int main()
{
cout << (check(10, 2));
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
int res = 0;
int checkRecursive( int num, int x, int k, int n)
{
if (x == 0)
res++;
int r = ( int ) floor ( pow (num, 1.0 / n));
for ( int i = k + 1; i <= r; i++) {
int a = x - ( int ) pow (i, n);
if (a >= 0)
checkRecursive(num, x - ( int ) pow (i, n), i, n);
}
return res;
}
int check( int x, int n)
{
return checkRecursive(x, x, 0, n);
}
int main()
{
printf ( "%d" , (check(10, 2)));
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class Solution {
static int res = 0 ;
static int checkRecursive( int num, int x, int k, int n)
{
if (x == 0 )
res++;
int r = ( int )Math.floor(Math.pow(num, 1.0 / n));
for ( int i = k + 1 ; i <= r; i++) {
int a = x - ( int )Math.pow(i, n);
if (a >= 0 )
checkRecursive(num,
x - ( int )Math.pow(i, n), i, n);
}
return res;
}
static int check( int x, int n)
{
return checkRecursive(x, x, 0 , n);
}
public static void main(String[] args)
{
System.out.println(check( 10 , 2 ));
}
}
|
Python3
def checkRecursive(num, rem_num, next_int, n, ans = 0 ):
if (rem_num = = 0 ):
ans + = 1
r = int (num * * ( 1 / n))
for i in range (next_int + 1 , r + 1 ):
a = rem_num - int (i * * n)
if a > = 0 :
ans + = checkRecursive(num, rem_num - int (i * * n), i, n, 0 )
return ans
def check(x, n):
return checkRecursive(x, x, 0 , n)
if __name__ = = '__main__' :
print (check( 10 , 2 ))
|
C#
using System;
class Solution {
static int res = 0;
static int checkRecursive( int num, int x,
int k, int n)
{
if (x == 0)
res++;
int r = ( int )Math.Floor(Math.Pow(num, 1.0 / n));
for ( int i = k + 1; i <= r; i++)
{
int a = x - ( int )Math.Pow(i, n);
if (a >= 0)
checkRecursive(num, x -
( int )Math.Pow(i, n), i, n);
}
return res;
}
static int check( int x, int n)
{
return checkRecursive(x, x, 0, n);
}
public static void Main()
{
Console.WriteLine(check(10, 2));
}
}
|
PHP
<?php
$res = 0;
function checkRecursive( $num , $x ,
$k , $n )
{
global $res ;
if ( $x == 0)
$res ++;
$r = (int) floor (pow( $num ,
1.0 / $n ));
for ( $i = $k + 1;
$i <= $r ; $i ++)
{
$a = $x - (int)pow( $i , $n );
if ( $a >= 0)
checkRecursive( $num , $x -
(int)pow( $i , $n ),
$i , $n );
}
return $res ;
}
function check( $x , $n )
{
return checkRecursive( $x , $x ,
0, $n );
}
echo (check(10, 2));
?>
|
Javascript
<script>
let res = 0;
function checkRecursive(num, x, k, n)
{
if (x == 0)
res++;
let r = Math.floor(Math.pow(num, 1.0 / n));
for (let i = k + 1; i <= r; i++) {
let a = x - Math.pow(i, n);
if (a >= 0)
checkRecursive(num,
x - Math.pow(i, n), i, n);
}
return res;
}
function check(x, n)
{
return checkRecursive(x, x, 0, n);
}
document.write(check(10, 2));
</script>
|
Time Complexity: O(x^(1/n)),
Space Complexity: O(log(x))
Simple Recursive Solution:
contributed by Ram Jondhale.
C++
#include <iostream>
#include<cmath>
using namespace std;
int getAllWaysHelper( int remainingSum, int power, int base){
int result = pow (base, power);
if (remainingSum == result)
return 1;
if (remainingSum < result)
return 0;
int x = getAllWaysHelper(remainingSum - result, power, base + 1);
int y = getAllWaysHelper(remainingSum, power, base+1);
return x + y;
}
int getAllWays( int sum, int power) {
return getAllWaysHelper(sum, power, 1);
}
int main()
{
int x = 10, n = 2;
cout << getAllWays(x, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int getAllWaysHelper( int remainingSum,
int power, int base)
{
int result = ( int )Math.pow(base, power);
if (remainingSum == result)
return 1 ;
if (remainingSum < result)
return 0 ;
int x = getAllWaysHelper(remainingSum - result,
power, base + 1 );
int y = getAllWaysHelper(remainingSum, power,
base + 1 );
return x + y;
}
public static int getAllWays( int sum, int power)
{
return getAllWaysHelper(sum, power, 1 );
}
public static void main(String[] args)
{
int x = 10 , n = 2 ;
System.out.print(getAllWays(x, n));
}
}
|
Python3
def getAllWaysHelper(remainingSum, power, base):
result = pow (base, power)
if (remainingSum = = result):
return 1
if (remainingSum < result):
return 0
x = getAllWaysHelper(remainingSum - result, power, base + 1 )
y = getAllWaysHelper(remainingSum, power, base + 1 )
return x + y
def getAllWays( sum , power):
return getAllWaysHelper( sum , power, 1 )
x,n = 10 , 2
print (getAllWays(x, n))
|
C#
using System;
class GFG {
static int getAllWaysHelper( int remainingSum, int power,
int bases)
{
int result = ( int )Math.Pow(bases, power);
if (remainingSum == result)
return 1;
if (remainingSum < result)
return 0;
int x = getAllWaysHelper(remainingSum - result,
power, bases + 1);
int y = getAllWaysHelper(remainingSum, power,
bases + 1);
return x + y;
}
static int getAllWays( int sum, int power)
{
return getAllWaysHelper(sum, power, 1);
}
public static int Main()
{
int x = 10, n = 2;
Console.Write(getAllWays(x, n));
return 0;
}
}
|
Javascript
<script>
function getAllWaysHelper(remainingSum, power, base)
{
let result = Math.pow(base, power);
if (remainingSum == result)
return 1;
if (remainingSum < result)
return 0;
let x = getAllWaysHelper(remainingSum - result, power, base + 1);
let y = getAllWaysHelper(remainingSum, power, base+1);
return x + y;
}
function getAllWays(sum, power) {
return getAllWaysHelper(sum, power, 1);
}
let x = 10, n = 2;
document.write(getAllWays(x, n));
</script>
|
Last Updated :
06 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...