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!
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
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.'
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)
Comment