# Thread: Help find the algorithm

1. ## Help find the algorithm

Hi all,

I have two numbers X and Y. The total of these two numbers cannot go over 500. I need a generic way of calculating that when (X * 3) + Y is greater than 500, I move part of X into Y so (X*3) + Y is <= 500 (preferably as close to 500 as possible).

For example, if X is 180 and Y is 0.

Moving 20 from X to Y gives
X = 160
Y = 20

(160*3) = 480 + Y(20) = 500

Can anybody help me work out an algorithm to calculate this?

Thanks for any help

2. Originally Posted by milkies
Hi all,

I have two numbers X and Y. The total of these two numbers cannot go over 500. I need a generic way of calculating that when (X * 3) + Y is greater than 500, I move part of X into Y so (X*3) + Y is <= 500 (preferably as close to 500 as possible).

For example, if X is 180 and Y is 0.

Moving 20 from X to Y gives
X = 160
Y = 20

(160*3) = 480 + Y(20) = 500

Can anybody help me work out an algorithm to calculate this?

Thanks for any help

Is this what you're looking for?

gap> LessThanFiveHundred:=function(X,Y)
> if X+Y > 500 then
> Print("X+Y cannot be greater than 500 \n");
> else
> while (3*X + Y)>500 do
> X:=X-1;
> Y:=Y+1;
> od;
> fi;
> Print("X = ", X, "\n");
> Print("Y = ", Y, "\n");
> Print(" 3*X + Y = ");
> return 3*X + Y;
> end;

3. gap> LessThanFiveHundred(160,80);
X = 130
Y = 110
3*X + Y = 500

LessThanFiveHundred(300,280);
X+Y cannot be greater than 500
X = 300
Y = 280
3*X + Y = 1180

Could fix that to have X+Y=580 as the last line, I suppose.. depends what you need...

gap> LessThanFiveHundred(300,-280);
X = 240
Y = -220
3*X + Y = 500

Also works for negative numbers...

And if you're below:
LessThanFiveHundred(100,80);
X = 100
Y = 80
3*X + Y = 380

As an exercise - what would you do to make it so that, for that last example, instead of staying with 380, how would you maximise it, but still stay under 500?

4. thankyou very much for the reply.

That is kind of what im looking for but it is more of an iterative approach rather than a formula for calculating it. For example the following does not work but should give a rough idea of what im after :-)

max_Value = 500 - Y
max_X = maxValue / 3 (maximum value X can be so X * 3 <= max_Value
move = X - max_X (amount to move from X onto Y)

X = X - move
Y = Y + move

5. What exactly are you trying to run/use it with?
Would you be willing to, say, use calculus?

6. Originally Posted by milkies
Hi all,

I have two numbers X and Y. The total of these two numbers cannot go over 500. I need a generic way of calculating that when (X * 3) + Y is greater than 500, I move part of X into Y so (X*3) + Y is <= 500 (preferably as close to 500 as possible).

For example, if X is 180 and Y is 0.

Moving 20 from X to Y gives
X = 160
Y = 20

(160*3) = 480 + Y(20) = 500

Can anybody help me work out an algorithm to calculate this?

Thanks for any help

What are you really trying to do?

What is wrong with y=500, x=0?

CB

7. Originally Posted by CaptainBlack
What are you really trying to do?

What is wrong with y=500, x=0?

CB
I think s/he's looking for a function that takes in any two integer values whose sum is less than 500, and find the smallest n possible such that ( X-n )* 3 + Y+n is less than 500.. then you assign X:= X-n, and Y:=Y+n...