2

I'm trying to make an efficient algorithm to find one of the solutions and how many solutions there are to the equation $$Ax+By+Cz=D$$ where $A,B,C,D\in \mathbb Z$ and the range for $x,y,z\in \mathbb Z$ are given by the user.

I saw this: https://math.stackexchange.com/a/20944/146115 answer but when I tried to apply it, it was wrong.

For example: $2x-3y-z=5$ in range $[-2,2]$ I know that it has 6 solutions but when I use the method in the above solution (2) I get: $\gcd (2,3)=1 \to w-z=5$ but this equation has no solutions in $[-2,2]$.

Is there another method to make this problem easier to solve, i.e, is there an efficient algorithm that will run on less than $O(n^3)$ runtime?

shinzou
  • 3,981

1 Answers1

2

If you calculate $z=\frac{D-Ax-By}{C}$ for all $x,y$ and then check if $z$ is an integer and in your range it will take $O(n^2)$.
And with the $\gcd$ method you won't gain anything on that.

vuur
  • 1,262
  • NVM, I made an error, it works. Nice. What do you mean by that the gcd method won't gain on that? – shinzou Nov 21 '14 at 18:08
  • The $\gcd$ method calculates an intermediate value $w=2x-3y$. So you would first calculate all $w$ ranging over $z$, then for every $w$ calculate i.e. $x$ by ranging over $y$. It is basically the same, only split in two steps. – vuur Nov 21 '14 at 18:12
  • I think I got it, well this is a lot more simple to implement. Finding one $z$ would only take $O(n)$ once I have one $x$ and $y$. So overall it would be $O(n^2)$. I wish it had a better runtime though... – shinzou Nov 21 '14 at 18:26