Skip navigation
fanglinyong
Currently Being Moderated

How to solve this equation?

May 24, 2012 6:37 AM

can anybody help me, solve this problem in as3 ?

 

x+y+z=8000;

0.17*x + 0.35*y +0.08*z =960

thanks very much!!!

 
Replies
  • Currently Being Moderated
    May 24, 2012 8:25 AM   in reply to fanglinyong

    I kinda wanna lean on rusty learning and say it can't be solved... three unknowns requires three equations. 

     

    As far as solving it with AS3, what do you mean?

     
    |
    Mark as:
  • Currently Being Moderated
    May 25, 2012 1:06 AM   in reply to fanglinyong

    AS3 for the above is

     

    for (var i:int = 0; i < 33; i++)

    {

              for (var j:int = 0; j < 12; j++)

              {

                        for (var k:int = 0; k < 300; k += 3)

                        {

                                  if ((i + j + k == 100) && (3*i + 8*j + k/3) == 100)

                                  {

                                            trace("x=", i, ", y=", j, ", z=", k);

                                  }

                        }

              }

    }

    trace("done");

     

    note 3 unknowns with 2 equations gives 2 solutions

     
    |
    Mark as:
  • Currently Being Moderated
    May 25, 2012 3:42 AM   in reply to fanglinyong

    this gives a little more explanation, although doesn't actually complete for me there are too many permitations

     

    you are also going to have to worry about the number representation issue using floating points numbers

     

    it might be better to convert the second equations to something equivalent which uses ints

     

    package

    {

              import flash.display.Sprite;

     

              public class Main extends Sprite

              {

                        //x+y+z=8000;

                        //0.17 * x + 0.35 * y +0.08 * z = 960

     

     

                        public function Main()

                        {

                                  // consider each equation in the form a.x + b.y + c.z = d

     

                                  // we have 3 unknowns x, y, z so we need 3 nested for loops to run over all possibilities

     

                                  // then the maximum value x can be is d/a

                                  // maximum y = d/b

                                  // maximum z = d/c

     

                                  // now if we have 2 equations we need to take the maximum for each known from either equation

                                  // so in your case with maximum x is the maximum of 8000/1 and 960/0.17 =

                                  // maximum y is the maximum of 8000/1 and 960/0.35

                                  // maximum z is the maximum of 8000/1 and 960/0.08

     

                                  // all these maximums should be rounded down to the nearest integer

                                  //max x = 8000

                                  //max y = 8000

                                  //max z = 12000

     

                                  //we use these maximum values for the nested for loop limits

     

                                  var maxX:int = Math.max(8000 / 1, 960 / 0.17);

                                  var maxY:int = Math.max(8000 / 1, 960 / 0.35);

                                  var maxZ:int = Math.max(8000 / 1, 960 / 0.08);

     

                                  trace("max values: ", maxX, maxY, maxZ);

     

                                  for (var i:int = 0; i < maxX; i++)

                                  {

                                            for (var j:int = 0; j < maxY; j++)

                                            {

                                                      for (var k:int = 0; k < maxZ; k++)

                                                      {

                                                                // this if statement is a representation of if the values for x, y and z we are trying

                                                                // actually solve the equations

                                                                // so in your case use the following

                                                                if ((i + j + k == 8000) && (0.17*i + 0.35*j + 0.08*k) == 960)

                                                                {

                                                                          trace("x=", i, ", y=", j, ", z=", k);

                                                                }

                                                      }

                                            }

                                  }

                                  trace("done");

                        }

              }

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points