Allocate based on

etharendil

New member
Joined
Nov 24, 2013
Messages
1
I'd like to split number e.g. 40 equally as much is possible between participants based on the level.
The result should be integer

John level 1
Ed level 4
Andre level 5
Eva level 6

what is
1+4+5+6=16
40/13=2,5

The result should be whole number so
John: 1x2,5 will have 2
Ed 4x2,5 will have 10
Andre 5x2,5 will have 12
Eva 6x2,5 will have 15

sum is 39 (1 left)
and that one which left should belong to Eva because she have highest level.
The number of the participants,base number(40) and the levels are various.
The base number might be e.g. 20 with 6 participants level 4 and one level 1.
This is kinda tricky for me and I'm not sure how to solve it. And I'd like to use it as function in program.
Do you have some idea how can I properly calculate this?
thank you very much
 
Last edited:
You understand that what you have written makes very little sense, don't you? You say " based on the level" but don't say what "based on" means! If it really means "proportional to" which is what you have tried to do, then it is NOT POSSIBLE with integers.

If "based on the level" does not strictly mean "proportional to" but "as close as proportional as you can do", you can start the way you did: the "weights" (your "levels") sum to 1+4+5+6=16
You then want to distribute as 1/16, 4/16, 5/16, and 6/16.
Okay, 40(1/16)= 2.5, 40(4/16)= 10, 40( 5/16)= 12.5, 40(6/16)= 15, just as you have.

But you want integer values. If you drop the "0.5" on 2.5 and 12.5, you have 2, 10, 12, and 15. But those add only to 39. What you want to do is round one of those "0.5"s down and the other up!

Which one? It really doesn't matter! There are a variety of "allocation algorithms" but there is no good reason here to pick one rather than the others.

So either 2, 10, 13, 15 or 3, 10, 12, 15. Obviously John and Andre will argue for one but they are both equally valid.
 
Top