Math Help Forum

Math Help Forum Feed Site Feed

Go Back   Math Help Forum > University Math Help > Other Advanced Topics
Reply
 
Thread Tools Display Modes
  #1  
Old July 29th, 2009, 04:47 AM
Newbie
 
Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
milkies is on a distinguished road
Default 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
Reply With Quote
Advertisement
 
  #2  
Old July 29th, 2009, 05:12 AM
Member
 
Join Date: Mar 2008
Posts: 153
Country:
Thanks: 30
Thanked 35 Times in 33 Posts
Unenlightened is on a distinguished road
Default

Quote:
Originally Posted by milkies View Post
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;
Reply With Quote
  #3  
Old July 29th, 2009, 05:18 AM
Member
 
Join Date: Mar 2008
Posts: 153
Country:
Thanks: 30
Thanked 35 Times in 33 Posts
Unenlightened is on a distinguished road
Default

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?
Reply With Quote
  #4  
Old July 29th, 2009, 05:21 AM
Newbie
 
Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
milkies is on a distinguished road
Default

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
Reply With Quote
  #5  
Old July 29th, 2009, 05:32 AM
Member
 
Join Date: Mar 2008
Posts: 153
Country:
Thanks: 30
Thanked 35 Times in 33 Posts
Unenlightened is on a distinguished road
Default

What exactly are you trying to run/use it with?
Would you be willing to, say, use calculus?
Reply With Quote
  #6  
Old July 29th, 2009, 06:30 AM
CaptainBlack's Avatar
Grand Panjandrum
 
Join Date: Nov 2005
Location: South of England
Posts: 11,379
Country:
Thanks: 667
Thanked 3,619 Times in 2,916 Posts
CaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond repute
Default

Quote:
Originally Posted by milkies View Post
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
Reply With Quote
  #7  
Old July 29th, 2009, 06:35 AM
Member
 
Join Date: Mar 2008
Posts: 153
Country:
Thanks: 30
Thanked 35 Times in 33 Posts
Unenlightened is on a distinguished road
Default

Quote:
Originally Posted by CaptainBlack View Post
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...
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -7. The time now is 09:07 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
©2005 - 2009 Math Help Forum


Math Help Forum is a community of maths forums with an emphasis on maths help in all levels of mathematics.
Register to post your math questions or just hang out and try some of our math games or visit the arcade.