It was not easy to come up with a short heading for this. I am sure that there are conditions for what I want to fulfill, and, without a doubt, a general algorithm for doing what I need, I still do not know about them.
I need to split the number into n parts, each of which is 50. The number itself is a multiple of 50. Here is an example: Divide 5,000 by 3 and get three numbers, each of which is less than 50:
I would also like the numbers to be distributed so that they flip back and forth, here is an example with a lot of numbers to illustrate this: Divide 5,000 by 7 and get 7 numbers, each of which is multiplied by 50:
Please note that in the above example, I am not worried that the extra 50 are not concentrated in the series, i.e. I do not need to have something like this:
- 700
- 700
- 750 <--- note that the "50s" are centered.
- 700
- 750 <--- note that the "50s" are centered.
- 700
- 700
I hope I asked this clearly enough so that you understand what I want to achieve.
Update: here is the function that I will use.
var number = 5000; var n = 7; var multiple = 50; var values = getIntDividedIntoMultiple(number, n, multiple) function getIntDividedIntoMultiple(dividend, divisor, multiple) { var values = []; while (dividend> 0 && divisor > 0) { var a = Math.round(dividend/ divisor / multiple) * multiple; dividend -= a; divisor--; values.push(a); } return values; }
source share