Hi,
as there are more variables than equations, there is no solution at all
(theoretically), or better, there are infinite number of values. However,
as there are all three variables integers and they are constrained (8-bit
vars), it is an integer programming problem I think with the goal to
minimize the difference between lhs and rhs. I am not sure it is a problem
to be solved with a PIC. A brute force approach may be used, though:
as Num is also constrained (I think so from the example), at least x
variable should be equal to 1. However, it would be easy to calculate the
upper bound for every variable, then use a brute force approach.
Let us see your example:
122 = x * ( y * (3*z + 4) + 4) + 18
104 = x * ( y * (3*z + 4) + 4)
| |
+------- a -------+
So, at first, determine the minimum of a! It is 4, if both y and z are
zero. So the upper bound for x is 104 / 4 = 26. The lower bound for x is
1.
The upper bound for a is 104 (when x = 1). Let introduce b = y * (3 * z +
4). The upper bound for b is 100. Obviously, y is at least 0 (for b = 0),
and y is at most 25 (if z is 0). Then, max of 3 * z is 96 if y = 1, so z
should lie between 0 and 32.
Now, the number of trials for the brute force method is 26 * 26 * 32 =
21632. It is nonetheless O((Num/4)³), but for a fast processor it may be
satisfying.
I hope this helps.
Regards,
Imre
PS: this is a q&d approach only, maybe there is a more exact solution.
On Mon, 8 Jul 2002, Thomas N wrote:
{Quote hidden}> Hello everyone,
>
> How to I search for x, y, and z variables in the equation below to get the
> answer that's closest to Num variable using C language?
>
> Num = x * ( y * (3*z + 4) + 4) + 18
>
> For example, if num = 122. The answer for x is 2, y is 3, z is 4.
>
> Best regards,
> Thomas
>
> --
>
http://www.piclist.com hint: The PICList is archived three different
> ways. See
http://www.piclist.com/#archives for details.
>
>
>
--
http://www.piclist.com hint: The PICList is archived three different
ways. See http://www.piclist.com/#archives for details.