How to divide a number into integer parts, each of which is a multiple of n?

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:

  • 1,650
  • 1700
  • +1650

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:

  • 700
  • 750
  • 700
  • 750
  • 700
  • 700
  • 700

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; } 
+4
source share
7 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 get 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 
+5
source
 // 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 will be

 f(5000, 50, 7) 

what gives

 [700,700,700,700,700,750,750] 
+2
source

Let a be your starting number, k the number of parts you want to split.
Suppose b = a / n.
Now you want to divide b into k integer integer parts.

  • Take k numbers, each of which is equal to b / k (integer division).
  • Add 1 to the first numbers of b% k.
  • Multiply each number by n.

Example: a = 5000, n = 50, k = 7.
b = 100
Starting series {14, 14, 14, 14, 14, 14, 14}
Add 1 to the first 2 integers {15, 15, 14, 14, 14, 14, 14}.
Multiply by 50 {750, 750, 700, 700, 700, 700, 700}.

+2
source

Your problem is the same as dividing the number X by N integer parts, all of which are within 1 of each other (just multiply everything by 50 after you find the result). This is easy to do - set all N numbers to Floor(X/N) , then add from 1 to X mod N from them.

+2
source

I see your problem mainly, trying to divide the amount of money into almost equal bundles of banknotes of a certain denomination.

For example, dividing $ 10,000 into 7 nearly equal packages of $ 50 bills.

 function getBundles(sum, denomination, count, shuffle) { var p = Math.floor(sum / denomination); var q = Math.floor(p / count); var r = p - q * count; console.log(r + " of " + ((q + 1) * denomination) + " and " + (count - r) + " of " + (q * denomination)); var b = new Array(count); for (var i = 0; i < count; i++) { b[i] = (r > 0 && (!shuffle || Math.random() < .5 || count - i == r) ? (--r, q + 1) : q) * denomination; } return b; } // Divide 10,000 dollars into 7 near-equal bundles of 50-dollar bills var bundles = getBundles(10000, 50, 7, true); console.log("bundles: " + bundles); 

Conclusion:

 4 of 1450 and 3 of 1400 bundles: 1400,1450,1450,1400,1450,1400,1450 

If the last argument to shuffle is true , it distributes the extra amount randomly between packages.

+2
source

Here is my trick:

 public static void main(String[] args) { System.out.println(toList(divide(50, 5000, 3))); System.out.println(toList(divide(50, 5000, 7))); System.out.println(toList(divide(33, 6600, 7))); } private static ArrayList<Integer> toList(int[] args) { ArrayList<Integer> list = new ArrayList<Integer>(args.length); for (int i : args) list.add(i); return list; } public static int[] divide(int N, int multiplyOfN, int partsCount) { if (N <= 0 || multiplyOfN <= N || multiplyOfN % N != 0) throw new IllegalArgumentException("Invalid args"); int factor = multiplyOfN / N; if (partsCount > factor) throw new IllegalArgumentException("Invalid args"); int parts[] = new int[partsCount]; int remainingAdjustments = factor % partsCount; int base = ((multiplyOfN / partsCount) / N) * N; for (int i = 0; i < partsCount; i ++) { parts[i] = (i % 2 == 1 && remainingAdjustments-- > 0) ? base + N : base; } return parts; } 
0
source

My algorithm provides an even distribution of residues in parts:

 function splitValue(value, parts, multiplicity) { var result = []; var currentSum = 0; for (var i = 0; i < parts; i++) { result[i] = Math.round(value * (i + 1) / parts / multiplicity) * multiple - currentSum; currentSum += result[i]; } return result; } 

For value = 5000, parts = 7, multiplicity = 50, it returns

 [ 700, 750, 700, 700, 700, 750, 700 ] 
0
source

Source: https://habr.com/ru/post/1402298/


All Articles