I need to split a number into similarly sized chunks. I have two variables, which are the number and the number of desired separate chunks.
So for example if I want to split 120 in 5 chunks, a good output could be: 0-24, 25-49, 50-74, 75-99, 100-120.
The chunks don't need to be completely identical in size, it's okay if they're similar, but they need to be completely separate, i.e. a number cannot be in the two chunks.
I have checked other answers like this but they don't work because they aren't completely separate.
This is my Java code:
int maxIndex = 10;
int numberOfChunks= 5;
int chunkSize =new Double( Math.floor( ((double)maxIndex) / numberOfChunks)).intValue();
int counter=0;
for (int i=0; i<numberOfChunks;i++) {
int min = counter;
int max = counter+chunkSize;
if (i==numberOfChunks-1) max=maxIndex;
counter+=chunkSize+1;
System.out.println(min + " - " + max);
}
This code does not work for small numbers because of that "+1". An example of the wrong output is:
0 - 2
3 - 5
6 - 8
9 - 11
12 - 10
Any ideas?