| 
July 29th, 2009, 04:47 AM
| | Newbie | | Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
| | 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 | 
July 29th, 2009, 05:12 AM
| | Member | | Join Date: Mar 2008
Posts: 153
Country: Thanks: 30
Thanked 35 Times in 33 Posts
| | Quote:
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; | 
July 29th, 2009, 05:18 AM
| | Member | | Join Date: Mar 2008
Posts: 153
Country: Thanks: 30
Thanked 35 Times in 33 Posts
| | 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? | 
July 29th, 2009, 05:21 AM
| | Newbie | | Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
| | 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 | 
July 29th, 2009, 05:32 AM
| | Member | | Join Date: Mar 2008
Posts: 153
Country: Thanks: 30
Thanked 35 Times in 33 Posts
| | What exactly are you trying to run/use it with?
Would you be willing to, say, use calculus? | 
July 29th, 2009, 06:30 AM
|  | Grand Panjandrum | | Join Date: Nov 2005 Location: South of England
Posts: 11,379
Country: Thanks: 667
Thanked 3,619 Times in 2,916 Posts
| | Quote:
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
__________________ Truth does not change because it is, or is not, believed by a majority of the people.
Giordano Bruno | 
July 29th, 2009, 06:35 AM
| | Member | | Join Date: Mar 2008
Posts: 153
Country: Thanks: 30
Thanked 35 Times in 33 Posts
| | Quote:
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... | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -7. The time now is 09:07 PM. | | |