Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide number into integer pieces that are each a multiple of n?

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;
}
like image 821
scubasteve Avatar asked Mar 19 '12 19:03

scubasteve


People also ask

How do you divide a number into N parts?

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.

How do you divide a number into 3 unequal parts?

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.

How do you divide a number into 4 parts?

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!


2 Answers

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
like image 136
sch Avatar answered Nov 12 '22 10:11

sch


// 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]
like image 20
Mike Samuel Avatar answered Nov 12 '22 09:11

Mike Samuel