I am trying to solve the following problem: For given n and α, determine the smallest number k for which n can be expressed as a sum of k numbers each of which is a perfect α-th power.
My first idea was to create a sequence of length n, then using recursion to create every possible sequence with the perfect α-th powers. After creating the sequence I would check to see if the sum of all the numbers in the sequence sum to n, then check if the total amount of numbers is less then k, if those are both true then I would update k. I wrote a program that solves most cases but for some reason doesn't create every possible sequence. For example if the user inputs 92 for n and 3 for α, then k should be 3 because 4^3 + 3^3 + 1^3 = 92, but my program returns k=92.
#include<iostream>
#include<cmath>
void check(const int &N, const int &A, int *pSeq, int position, int &K){
int sum=0;
int total=0;
for(int i=0; i<N; ++i){
sum+=pSeq[i];
if(pSeq[i]>0){
++total;
}
}
if(sum==N){
if(total < k){
K=total;
std::cout<<"Print seq: ";
for(int i =0; i<N; ++i){
std::cout<<pSeq[i] <<" ";
}
std::cout<<std::endl;
}
}
if(sum<N){
if(position < N){
for(int i=0; pow(i, A)<N+1; ++i){
pSeq[position]=pow(i, A);
check(N, A, pSeq, position+1, K);
}
}
}
}
int main(){
int n, a;
std::cout<<"Enter n and a: ";
std::cin>>n >>a;
int k=n;
int *sequence=new int[n];
for(int i=0; i<n; ++i){
sequence[i]=0;
}
check(n, a, sequence, 0, k);
std::cout<<"k=" <<k <<std::endl;
return 0;
}