• 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

    #11
    Piece of piss. Proof is a bit long for posting though.

    Fermat.
    What happens in General, stays in General.
    You know what they say about assumptions!

    Comment


      #12
      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 ?




      In most programming languages there is modulus operator that yiou can use to do you're rounding operations.

      Let me show you illustartion.

      Number_to_round <modulus operator> reminder_value
      for very simple example

      if (100 % 2 == 0)
      // the number is less than 50 so round down to 0
      else
      // the number is greater than 50 so round up t o 100

      I think from this simple example you should be able to accomodate this in your project and use this with some degree of success.

      Let me know if I can be of any further assistance.

      HTH,

      Joshi.

      Comment


        #13
        Originally posted by Hill Station Murthy View Post

        HTH,

        Joshi.
        Not really, Bob, in fact it's practically irrelevant; but thanks for trying.
        Work in the public sector? Read the IR35 FAQ here

        Comment


          #14
          veeeeeeeeeery quick answer cos hungry and glanced at thread
          add all the rounded numbers up
          =999

          now you've got to loose 1 somewhere so choose the number with the fractional part closest to .5 and > 0.5 and zap it's fractional part

          no repeat the adds of the rounded numbers and u get 998.

          P.S. There might (probably) is a fairer way to "allocate" the zapping of the extra 1 but what I just scribbled might be a good way

          Comment


            #15
            Originally posted by EternalOptimist View Post
            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 ?
            As it all boils down to 'reasonableness', there's probably some AI algorithm that would do it for you.

            Comment


              #16
              Originally posted by Olly View Post
              veeeeeeeeeery quick answer cos hungry and glanced at thread
              add all the rounded numbers up
              =999

              now you've got to loose 1 somewhere so choose the number with the fractional part closest to .5 and > 0.5 and zap it's fractional part

              no repeat the adds of the rounded numbers and u get 998.

              P.S. There might (probably) is a fairer way to "allocate" the zapping of the extra 1 but what I just scribbled might be a good way
              aye, thats the logic all right. but I want the algorithm




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

              Comment


                #17
                Originally posted by EternalOptimist View Post
                aye, thats the logic all right. but I want the algorithm




                you still haven't found your way to the pub then?
                And what exactly is wrong with an "ad hominem" argument? Dodgy Agent, 16-5-2014

                Comment


                  #18
                  ok part way through shepherds' pie so can spare a few more ticks but I'm not going to check this mmmmmmmmkay

                  array_percentages (0.2,0.1,0.7)
                  array_numbers ()
                  target = 998
                  tobezapped
                  bestsofar = 0

                  for n = 1 to max_elements(array_percentages)
                  array_numbers(n) = array_percentages(n) * target
                  next n

                  while sum(integer_part(array_numbers())) <> target
                  if sum(integer_part(array_numbers())) > target
                  for for n = 1 to max_elements(array_numbers)
                  if fractional_part (array_numbers(n)) - 0.5 >= bestsofar then
                  bestsofar = fractional_part (array_numbers(n)) - 0.5
                  tobezapped = n
                  end if
                  next n
                  array_numbers(tobezapped)=integer_part(array_numbe rs(tobezapped)) 'or you could subtract bestsofar+1
                  else
                  'do the sort of opposite to round up the number closest to fractional 0.5
                  end if
                  wend

                  now your answer is integer_part of all the elements in array_numbers
                  Last edited by Olly; 2 December 2011, 18:15.

                  Comment


                    #19
                    Originally posted by Olly View Post
                    veeeeeeeeeery quick answer cos hungry and glanced at thread
                    add all the rounded numbers up
                    =999

                    now you've got to loose 1 somewhere so choose the number with the fractional part closest to .5 and > 0.5 and zap it's fractional part

                    no repeat the adds of the rounded numbers and u get 998.

                    P.S. There might (probably) is a fairer way to "allocate" the zapping of the extra 1 but what I just scribbled might be a good way
                    Whichever rounding scheme you choose you will introduce a rounding error. Either the totals will not add up in some cases or you have to accept different numerical values for identical percentages in other cases. The best way to distribute the error is probably to "dither" the untruncated numerical values before rounding although I suspect this won't actually achieve what you want.
                    While you're waiting, read the free novel we sent you. It's a Spanish story about a guy named 'Manual.'

                    Comment


                      #20
                      Originally posted by EternalOptimist View Post
                      the number of variables is an unknown
                      the % will be 2 dp
                      n will be an integer


                      Unknown? I thought you started with a given list of percentages (integers or otherwise) which in effect you had to scale up and round such their sum is some given integer and they deviate minimally as percentages of this sum from the originals.

                      That's the problem whose solution I sketched. But if you don't even know what the percentages are to start with it's an ill-posed problem.

                      (although if you know all the distinct percentages but not the number of repetitions of each, then it's a variant of the Frobenius Coin Problem)
                      Work in the public sector? Read the IR35 FAQ here

                      Comment

                      Working...
                      X