For this equation, I need to count possible solutions:
$$ a + b^2 + c^3 + d^4 \le S $$
$$0\le a,b,c,d \le S$$
I tried the following approach:
Integer S=Integer.parseInt(br.readLine());
// get maximum possible value of a,b,c,d which satisfy equation
int a=S;
int b=(int) Math.sqrt(a);
int d=(int) Math.sqrt(b);
int c=(int) Math.cbrt(a);
int count=0;
for (int i = 0; i <= a; i++) {
for (int j = 0; j <= b; j++) {
for (int k = 0; k <= c; k++) {
for (int l = 0; l <= d; l++) {
int total=i+j*j+(int)Math.pow(k, 3)+
(int)Math.pow(l,4);
if(total<=a) {
count++;
}
}
}
}
}
System.out.println(count);
Time complexity: \$O(n \times \sqrt{n} \times \sqrt[3]{n} \times \sqrt[4]{n})\$ (Not sure)
For large value, this takes time. Can someone please help me with better solution with time complexity?
a,b,c,delements of the natural numbers ? Can they be negative, or even complex? Please update the problem statement :) \$\endgroup\$