Let's rewrite your equation as y2 = z - 3x2. Then y = sqrt(z - 3x2). We can easily see that there are no solutions for z < 3. Now we can write the following code (is C code, but it's easy to port it to another language):
int f(int z)
{
int result = 0;
int x = 0;
double m = 0; // m is 3*x*x
double y;
if (z < 3) return 0; // You could remove this line (the function will continue to work properly)
while (m <= z)
{
y = sqrt(z - m);
// Test if y is integer...
if (y == floor(y)) result++;
x++;
m = 3\*x\*x;
}
return result;
}
Note that this function has a complexity of O(z-2) as it checks for all possible integers values of x. Also note that this implementation accept solutions where x and/or y are zero (e.g. f(4) returns 2 and the solutions found are { 0, 2 } and { 1 ,1 }). If you want solutions with x and y stricly positive you should initialize the loop with x = 1
and m = 3
, and inside the loop ignore solutions with y == 0
.