• 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

EternalOptimist

The beerded one
Thanks received
191
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 ?




:rolleyes:
 
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 ?



:rolleyes:

Yes.

Is task hard?

Yes---> delegate and invoice
No---> delegate, invoice and go to pub
Else---> delegate, invoice and go to pub
 
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
 
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.
 
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%

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!
 
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 :rolleyes:

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: 11:50
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 :rolleyes:

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


:rolleyes:
 

Advertisers

Back
Top