Find ways an Integer can be expressed as sum of n-th power of unique natural numbers

Given two numbers x and n, find 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to count number of ways any
// given integer x can be expressed as n-th
// power of unique natural numbers.
#include<bits/stdc++.h>
using namespace std;
  
// Function to calculate and return the
// power of any given number
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);
}
  
// Function to check power representations recursively
int checkRecursive(int x, int n, int curr_num = 1,
                   int curr_sum = 0)
{
    // Initialize number of ways to express
    // x as n-th powers of different natural
    // numbers
    int results = 0;
  
    // Calling power of 'i' raised to 'n'
    int p = power(curr_num, n);
    while (p + curr_sum < x)
    {
        // Recursively check all greater values of i
        results += checkRecursive(x, n, curr_num+1,
                                        p+curr_sum);
        curr_num++;
        p = power(curr_num, n);
    }
  
    // If sum of powers is equal to x
    // then increase the value of result.
    if (p + curr_sum == x)
        results++;
  
    // Return the final result
    return results;
}
  
// Driver Code.
int main()
{
    int x = 10, n = 2;
    cout << checkRecursive(x, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count number of ways any 
// given integer x can be expressed as n-th 
// power of unique natural numbers. 
  
class GFG
{
      
// Function to calculate and return the 
// power of any given number 
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); 
  
// Function to check power representations recursively 
static int checkRecursive(int x, int n, int curr_num,int curr_sum) 
    // Initialize number of ways to express 
    // x as n-th powers of different natural 
    // numbers 
    int results = 0
  
    // Calling power of 'i' raised to 'n' 
    int p = power(curr_num, n); 
    while (p + curr_sum < x) 
    
        // Recursively check all greater values of i 
        results += checkRecursive(x, n, curr_num + 1
                                        p + curr_sum); 
        curr_num++; 
        p = power(curr_num, n); 
    
  
    // If sum of powers is equal to x 
    // then increase the value of result. 
    if (p + curr_sum == x) 
        results++; 
  
    // Return the final result 
    return results; 
  
// Driver Code.
public static void main (String[] args) 
{
    int x = 10, n = 2
    System.out.println(checkRecursive(x, n, 1, 0));
}
  
// This code is contributed by mits

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to count number of ways any
# given integer x can be expressed as n-th
# power of unique natural numbers.
  
# Function to calculate and return the
# power of any given number
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)
  
# Function to check power representations recursively
def checkRecursive(x, n, curr_num=1, curr_sum=0):
  
    # Initialize number of ways to express
    # x as n-th powers of different natural
    # numbers
    results = 0
  
    # Calling power of 'i' raised to 'n'
    p = power(curr_num, n)
    while(p + curr_sum < x):
  
        # Recursively check all greater values of i
        results += checkRecursive(x, n, curr_num + 1, p + curr_sum)
        curr_num = curr_num + 1
        p = power(curr_num, n)
  
    # If sum of powers is equal to x
    # then increase the value of result.
    if(p + curr_sum == x):
        results = results + 1
  
    # Return the final result
    return results
  
# Driver Code.
if __name__=='__main__':
    x = 10
    n = 2
    print(checkRecursive(x, n))
  
  
# This code is contributed by
# Sanjit_Prasad

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count number of ways any 
// given integer x can be expressed as 
// n-th power of unique natural numbers. 
using System;
  
class GFG
{
      
// Function to calculate and return 
// the power of any given number 
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); 
  
// Function to check power
// representations recursively 
static int checkRecursive(int x, int n, 
                          int curr_num,
                          int curr_sum) 
    // Initialize number of ways to express 
    // x as n-th powers of different natural 
    // numbers 
    int results = 0; 
  
    // Calling power of 'i' raised to 'n' 
    int p = power(curr_num, n); 
    while (p + curr_sum < x) 
    
        // Recursively check all greater values of i 
        results += checkRecursive(x, n, curr_num + 1, 
                                        p + curr_sum); 
        curr_num++; 
        p = power(curr_num, n); 
    
  
    // If sum of powers is equal to x 
    // then increase the value of result. 
    if (p + curr_sum == x) 
        results++; 
  
    // Return the final result 
    return results; 
  
// Driver Code.
public static void Main() 
{
    int x = 10, n = 2; 
    System.Console.WriteLine(checkRecursive(x, n, 1, 0));
}
  
// This code is contributed by mits

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to count 
// number of ways any
// given integer x can
// be expressed as n-th
// power of unique 
// natural numbers.
  
// Function to calculate and return
// the power of any given number
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 to check power
// representations recursively
function checkRecursive($x, $n,
                        $curr_num = 1, 
                        $curr_sum = 0)
{
      
    // Initialize number of
    // ways to express
    // x as n-th powers 
    // of different natural
    // numbers
    $results = 0;
  
    // Calling power of 'i' 
    // raised to 'n'
    $p = power($curr_num, $n);
    while ($p + $curr_sum < $x)
    {
          
        // Recursively check all
        // greater values of i
        $results += checkRecursive($x, $n
                                   $curr_num + 1,
                                   $p + $curr_sum);
        $curr_num++;
        $p = power($curr_num, $n);
    }
  
    // If sum of powers 
    // is equal to x
    // then increase the
    // value of result.
    if ($p + $curr_sum == $x)
        $results++;
  
    // Return the final result
    return $results;
}
  
// Driver Code.
$x = 10; $n = 2;
echo(checkRecursive($x, $n));
  
// This code is contributed by Ajit.
?>

chevron_right


Output:

1

Alternate Solution :

Below is an alternate simpler solution provided by Shivam Kanodia.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find number of ways to express 
// a number as sum of n-th powers of numbers.
#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;
}
  
// Wrapper over checkRecursive()
int check(int x, int n)
{
    return checkRecursive(x, x, 0, n);
}
  
// Driver Code
int main()
    cout << (check(10, 2));
    return 0;
}
  
// This code is contributed by mits

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find number of ways to express a
// number as sum of n-th powers of numbers.
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;
    }
      
    // Wrapper over checkRecursive()
    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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find number of ways to express 
# a number as sum of n-th powers of numbers.
from math import pow, floor
  
res = 0
def checkRecursive(num, x, k, n):
    global res
    if (x == 0):
        res += 1
      
    r = floor(pow(num, (1 / n)));
  
    for i in range(k + 1, r + 1, 1):
        a = x - int(pow(i, n))
        if (a >= 0):
            checkRecursive(num, x - int(pow(i, n)), i, n)
    return res
  
# Wrapper over checkRecursive()
def check(x, n):
    return checkRecursive(x, x, 0, n)
  
# Driver Code
if __name__ == '__main__':
    print(check(10, 2))
          
# This code is contributed by
# Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find number of
// ways to express a number as sum
// of n-th powers of numbers.
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;
    }
      
    // Wrapper over checkRecursive()
    static int check(int x, int n)
    {
        return checkRecursive(x, x, 0, n);
    }
      
    // Driver code
    public static void Main()
    
        Console.WriteLine(check(10, 2));
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find number 
// of ways to express a number 
// as sum of n-th powers of numbers.
$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;
}
  
// Wrapper over
// checkRecursive()
function check($x, $n)
{
    return checkRecursive($x, $x
                          0, $n);
}
  
// Driver Code
echo (check(10, 2));
  
// This code is contributed by ajit
?>

chevron_right


Output:

1

This article is contributed by DANISH KALEEM. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.