• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Maths problem

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    Maths problem

    I have a number and I want to break it down into integers by %

    but I want to guarantee that the sum of the breakdown equals the original number. i.e. if there is a rounding error, I want it corrected.

    eg

    number 998

    10% = 100 (rounded up)
    30% = 300 (rounded up)
    30% = 300 (rounded up)
    30% = 300 (rounded up)

    for a total of 1000

    I can do it the hard way. but is there a simple algorithm ?




    (\__/)
    (>'.'<)
    ("")("") Born to Drink. Forced to Work

    #2
    Originally posted by EternalOptimist View Post
    I have a number and I want to break it down into integers by %

    but I want to guarantee that the sum of the breakdown equals the original number. i.e. if there is a rounding error, I want it corrected.

    eg

    number 998

    10% = 100 (rounded up)
    30% = 300 (rounded up)
    30% = 300 (rounded up)
    30% = 300 (rounded up)

    for a total of 1000

    I can do it the hard way. but is there a simple algorithm ?



    Yes.

    Is task hard?

    Yes---> delegate and invoice
    No---> delegate, invoice and go to pub
    Else---> delegate, invoice and go to pub
    And what exactly is wrong with an "ad hominem" argument? Dodgy Agent, 16-5-2014

    Comment


      #3
      Just alternate the round up or round downs?
      It's about time I changed this sig...

      Comment


        #4
        Originally posted by Mich the Tester View Post
        Yes.

        Is task hard?

        Yes---> delegate and invoice
        No---> delegate, invoice and go to pub
        Else---> delegate, invoice and go to pub



        I always end up buying all the beers. Now THATS what I call a round-in error
        (\__/)
        (>'.'<)
        ("")("") Born to Drink. Forced to Work

        Comment


          #5
          Originally posted by EternalOptimist View Post
          I have a number and I want to break it down into integers by %

          but I want to guarantee that the sum of the breakdown equals the original number. i.e. if there is a rounding error, I want it corrected.

          eg

          number 998

          10% = 100 (rounded up)
          30% = 300 (rounded up)
          30% = 300 (rounded up)
          30% = 300 (rounded up)

          for a total of 1000

          I can do it the hard way. but is there a simple algorithm ?
          do as you have just there then starting with the largest, subtract 1 from each % result untill you are back down to your original number

          so:
          number 998
          Pass 1:
          10% = 100 (rounded up)
          30% = 300 (rounded up)
          30% = 300 (rounded up)
          30% = 300 (rounded up)
          total = 1000

          Pass 2:
          10% = 100 (rounded up)
          30% = 300 (rounded up)
          30% = 300 (rounded up)
          30% = 299 (rounded up and -1)
          total = 999

          Pass 2:
          10% = 100 (rounded up)
          30% = 300 (rounded up)
          30% = 299 (rounded up and -1)
          30% = 299 (rounded up and -1)

          total = 998
          Coffee's for closers

          Comment


            #6
            Originally posted by EternalOptimist View Post
            I can do it the hard way. but is there a simple algorithm ?
            In your example you either round the 30% down to 299 or up to 300 and that makes you off by at least 1. I don't think you can legitimately say that 30% of 998 is 299 sometimes and 300 other times, and I don't see how you can argue that 10% of 998 is 101, so you are ****ed.

            The correct answer is more precision.
            While you're waiting, read the free novel we sent you. It's a Spanish story about a guy named 'Manual.'

            Comment


              #7
              Originally posted by Mich the Tester View Post
              Yes.

              Is task hard?

              Yes---> delegate and invoice
              No---> delegate, invoice and go to pub
              Else---> delegate, invoice and go to pub
              Delayed apology to programming purists for using the 'go to' command. Now you know why I'm a tester.
              And what exactly is wrong with an "ad hominem" argument? Dodgy Agent, 16-5-2014

              Comment


                #8
                Originally posted by doodab View Post
                In your example you either round the 30% down to 299 or up to 300 and that makes you off by at least 1.
                in which case add 1 to the 10%

                Originally posted by doodab View Post
                I don't think you can legitimately say that 30% of 998 is 299 sometimes and 300 other times
                Its a square peg - round hole problem, the question is how much rounding can you apply to a square before it stops being a square!
                Coffee's for closers

                Comment


                  #9
                  I'll summarize for 3 variables, but it works the same for any positive number of these

                  Suppose your given exact percentages are p, q, r

                  Let x, y, z be your required integers with sum s

                  1. Determine x, y, z as floor(p * s / 100), floor(q * s / 100), floor(r * s / 100)

                  2. If the sum x + y + z is less than s then add 1 to x, y, z successively until
                  the sum equals s

                  3. The required solution is whichever of the following minimizes
                  (X - p * s / 100)^2 + (Y - q * s / 100)^2 + (Z - r * s / 100)^2

                  X, Y, Z =
                  x, y, z or
                  x + 1, y - 1, z or
                  x + 1, y, z - 1 or
                  x - 1, y + 1, z or
                  x, y + 1, z - 1 or
                  x - 1, y, z + 1 or
                  x, y - 1, z + 1


                  I think

                  For two variables the choice of X, Y would be one of x, y; x-1, y+1; x+1, y-1.

                  In general one shuffles a 1 between every pair (in either direction) and includes the original set in the test (DOH! I forgot that)
                  Last edited by OwlHoot; 2 December 2011, 12:32.
                  Work in the public sector? Read the IR35 FAQ here

                  Comment


                    #10
                    Originally posted by OwlHoot View Post
                    I'll summarize for 3 variables, but it works the same for any positive number of these

                    Suppose your given exact percentages are p, q, r

                    Let x, y, z be your required integers with sum s

                    1. Determine x, y, z as floor(p * s / 100), floor(q * s / 100), floor(r * s / 100)

                    2. If the sum x + y + z is less than s then add 1 to x, y, z successively until
                    the sum equals s

                    3. The required solution is whichever of the following minimizes
                    (X - p * s / 100)^2 + (Y - q * s / 100)^2 + (Z - r * s / 100)^2

                    X, Y, Z =
                    x + 1, y - 1, z or
                    x + 1, y, z - 1 or
                    x - 1, y + 1, z or
                    x, y + 1, z - 1 or
                    x - 1, y, z + 1 or
                    x, y - 1, z + 1


                    I think
                    the number of variables is an unknown
                    the % will be 2 dp
                    n will be an integer


                    (\__/)
                    (>'.'<)
                    ("")("") Born to Drink. Forced to Work

                    Comment

                    Working...
                    X