I was solving the power sum problem (aka all possible ways problem in coding ninja).
Given two integers a and b. You need to find and return the count of possible ways in which we can represent the number a as the sum of unique integers raise to the power b.
Below is the code for that:
#include <iostream>
#include <cmath>
using namespace std;
int getAllWays(int b, int li, int rem)
{
if (rem==0)
{
return 1;
}
if (li<=0)
{
return 0;
}
int remrt = pow(rem,(1.0/b));
while(remrt>=li)
{
remrt--;
}
// Select case
int v1 = getAllWays(b, remrt, rem-pow(remrt,b));
// Reject case
int v2 = getAllWays(b, remrt, rem);
return v1+v2;
}
int getAllWays(int a, int b)
{
// Write your code here
int li = pow(a,(1.0/b));
li++;
return getAllWays(b, li, a);
}
int main()
{
int a, b;
cin >> a >> b;
cout <<getAllWays(a, b);
}
For input 100 2
I'm getting output 4 in my visual studio code whereas output 3 in online compilers such as Jdoodle and ideone.com.
Correct output is 3.
What could be the reason? My terminal shows g++ version as below:
C:\Users\username>g++ --version
g++ (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Ideone link: https://ideone.com/6nDaQI