Results 1 to 7 of 7

Math Help - Help find the algorithm

  1. #1
    Newbie
    Joined
    Jul 2009
    Posts
    2

    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
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    Mar 2008
    Posts
    182
    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;
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Member
    Joined
    Mar 2008
    Posts
    182
    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?
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Jul 2009
    Posts
    2
    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
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Member
    Joined
    Mar 2008
    Posts
    182
    What exactly are you trying to run/use it with?
    Would you be willing to, say, use calculus?
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    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
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Member
    Joined
    Mar 2008
    Posts
    182
    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...
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 2
    Last Post: December 14th 2011, 07:30 AM
  2. find the k'th smallest in array algorithm
    Posted in the Math Software Forum
    Replies: 1
    Last Post: December 12th 2011, 10:25 AM
  3. Replies: 2
    Last Post: August 17th 2010, 09:43 AM
  4. Replies: 4
    Last Post: March 4th 2010, 01:09 PM
  5. Algorithm to find the median number
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: January 4th 2007, 06:33 AM

Search Tags


/mathhelpforum @mathhelpforum