Had a hard time coming up with a concise title for this. I’m sure there are terms for what I want to accomplish and there is no doubt a common algorithm to accomplish what I’m after – I just don’t know about them yet.
I need to break up a number into n pieces that are each a multiple of 50. The number is itself a multiple of 50. Here is an example:
Divide 5,000 by 3 and end up with three numbers that are each multiples of 50:
- 1,650
- 1,700
- 1,650
I also would like to have the numbers distributed so that they flip back and forth, here is an example with more numbers to illustrate this:
Divide 5,000 by 7 and end up with 7 numbers that are each multiples of 50:
- 700
- 750
- 700
- 750
- 700
- 700
- 700
Note that in the above example I’m not worried that the extra 50 is not centered in the series, that is I don’t need to have something like this:
- 700
- 700
- 750 <— note the ’50s’ are centered
- 700
- 750 <— note the ’50s’ are centered
- 700
- 700
Hopefully I’ve asked this clearly enough that you understand what I want to accomplish.
Update: Here is the function I’ll be using.
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;
}
Edit
You can alternate
Math.floorandMath.ceilto obtain the desired result: