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:
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:
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:
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;
}
Function divideN(int n) takes n and returns the number of ways in which n can be divided into 3 parts. Take the initial variable count as 0 for the number of ways. Traverse using three for loops for each part of the number. Outermost loop from 1<=i<n, inner loop i<=j<n , innermost j<=k<n.
Let the number be p. It is to be divided into three parts in the ratio a : b : c. Therefore, x = ak = apa+b+c, y = bk = bpa+b+c, z = ck = cpa+b+c.
There is a trick you can use to divide by 4: the rule is to divide by 2 twice. For example, if you want to divide 12 by 4, you simply divide 12 by 2, which gives you 6, and then divide that number by 2, which, in this case, gives you 3. Easy!
var number = 5000;
var n = 7;
var values = [];
while (number > 0 && n > 0) {
var a = Math.floor(number / n / 50) * 50;
number -= a;
n--;
values.push(a);
} // 700 700 700 700 700 750 750
Edit
You can alternate Math.floor
and Math.ceil
to obtain the desired result:
while (number > 0 && n > 0) {
if (a%2 == 0)
a = Math.floor(number / n / 50) * 50;
else
a = Math.ceil(number / n / 50) * 50;
number -= a;
n--;
values.push(a);
} // 700 750 700 750 700 700 700
// i - an integer multiple of k
// k - an integer
// n - a valid array length
// returns an array of length n containing integer multiples of k
// such that the elements sum to i and the array is sorted,
// contains the minimum number of unique elements necessary to
// satisfy the first condition, the elements chosen are the
// closest together that satisfy the first condition.
function f(i, k, n) {
var minNumber = (((i / k) / n) | 0) * k;
var maxNumber = minNumber + k;
var numMax = (i - (minNumber * n)) / k;
var nums = [];
for (var i = 0; i < n - numMax; ++i) {
nums[i] = minNumber;
}
for (var i = n - numMax; i < n; ++i) {
nums[i] = maxNumber;
}
return nums;
}
So your second example would be
f(5000, 50, 7)
which yields
[700,700,700,700,700,750,750]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With