Searching \ for '[OT]: C Question. Please help!' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/language/index.htm?key=c
Search entire site for: 'C Question. Please help!'.

Exact match. Not showing close matches.
PICList Thread
'[OT]: C Question. Please help!'
2002\07\08@031035 by Thomas N

picon face
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.


2002\07\08@035545 by dr. Imre Bartfai

flavicon
face
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}

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\07\08@120103 by Scott Dattalo

face
flavicon
face
On Mon, 8 Jul 2002, Thomas N wrote:

> 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.

This wouldn't have anything to do with loop delay calculation by any
chance? :)

You've stumbled onto a Diophantine equation. In your subsequent email you
stated that x,y, and z are constrained to 8-bit unsigned integers. This
limits the number of solutions. However, just because you have three
parameters, it doesn't gurantee that you can find an (x,y,z) triplet that
will map to all integers in some range Nmin to Nmax. Solving these types
of equations in general is much more difficult than solving algebraic
equations. Maybe after reading TAOCP:

http://www-cs-faculty.stanford.edu/~knuth/taocp.html

you'll be able to solve it no time!

Now, if this is a loop calculation, I'd suggest rearranging the loop so
that the loop delay is a function of one parameter. There are several that
exist. For example, the code generated by Nicolai's delay generator
produces something like:

delay:
       movlw   X1
       movwf   x
       movlw   Y1
       movwf   y
       movlw   Z1
       movwf   z

loop    decfsz  z,f
        goto   $+2
       decfsz  y,f
        goto   $+2
       decfsz  x,f
        goto   loop

       return

The time for this loop to execute is:

for x,y,z all greater than zero:

Delay =   (7*z' - 1) + (256*7)*y' + (256*256*7)*x'  + 6 + 4
     =  7* N + 9

Where, x' = x-1, y' = y-1, and z' = z-1, and N = x':y':z' is a 24-bit
integer.

(There maybe an error in the above equation that I'm sure someone will
point out).

From this, it's much easier to calculate the integers x,y,z given a
desired delay.

Please note that the minimum time doesn't occur for x,y, and z all zero.
Instead it occurs for x,y, and z equal to 1. If x,y, or z are 0, then
replace them with 256 in the Delay equation.

Scott

PS. I hope this code isn't being used a timer for bomb.

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\07\08@124737 by Bill & Pookie

picon face
Note ************
I do not know how others will react to this kind
of topic, but I expect to find out.  I realize
that it would be bad if the list filled up with
non pic programming questions from students.
****************

Looks to me like it is a class assignment in a c
programming class.  And yes, some students get on
the net and expect to find someone to write and
debug their assignments for them by two o'clock
the same day.

And then others do want to learn and are willing
to put effort into the assignment.

I think people here would help, I know I would.
But you will need to show some effort.

The first thing is to reduce that formula so you
can understand it better. I would assume that you
are studying loops and/or nested loops.  That is a
big hint as to the program.

What is the range the variables can take?  I
assume that none can be zero, so 1 is the minimum
value for all, and none can be greater than 255.
But surly a better maximum than 255 can be found
for each variable.

A lot of programming is just looking at the
problem and thinking about it.  When you get the
concept down to the point where you can explain it
and they say "What's so complicated about that?",
then you are ready to put "keys to the screen".

So reduce the formula and attempt to find some
better max values that 255.  Then come back with
that information.

Programming can be fun.  Enjoy it.

Bill



> On Mon, 8 Jul 2002, Thomas N wrote:
>
> > 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.

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


More... (looser matching)
- Last day of these posts
- In 2002 , 2003 only
- Today
- New search...