Divide the number range by a certain number of intervals

I have an interval [0; max] [0; max] , and I want to break it into a certain number of sub-intervals. To do this, I wrote a function called getIntervalls(max, nbIntervals) , where max is the maximum element in my first interval, and nbIntervals is the number of expected auxiliary intervals.

For instance:

  • getIntervalls(3, 2) should return [[0,1], [2,3]] ,
  • getIntervalls(6, 2) should return [[0,3], [4,6]] ,
  • getIntervalls(8, 3) should return [[0,2], [3,5], [6,8]] ,
  • getIntervalls(9, 3) should return [[0,3], [4,7], [8,9]] ,

Here is my function:

 function getIntervalls(max, nbIntervalls) { var size = Math.ceil(max / nbIntervalls); var result = []; if (size > 1) { for (let i = 0; i < nbIntervalls; i++) { var inf = i + i * size; var sup = inf + size < max ? inf + size: max; result .push([inf, sup]); } } else { result.push([0, max]); } return result; } console.log(JSON.stringify(getIntervalls(7, 2))); 

It works correctly and shows this output:

 [[0,4],[5,7]] 

When I change the parameters to 7 and 3, it shows:

 [[0,3],[4,7],[8,7]] 

instead

 [[0,2],[3,5],[6,7]] 

Can anybody help me? Thank you in advance. ES6 syntax will be appreciated! :)

+5
source share
3 answers

You need to use Math.round() to accept the nearest decimal integer of the size interval. In addition, you need to reduce one max calculation for size in order to take into account the effective number of intervals.

Here you can change the code:

 function getIntervalls(max, nbIntervalls) { var size = Math.round((max-1) / nbIntervalls); var result = []; for (let i = 0; i < nbIntervalls; i++) { var inf = i + i * size; var sup = inf + size < max ? inf + size: max; result.push([inf, sup]); if(inf >= max || sup >= max)break; } return result; } 

Note that this corresponds to the desired number of intervals, so there may be some case of a pair of numbers
[..., [n-2,n-1], [n,n]] .

Hope this helps!

+4
source

You need to change the size calculation from Math.ceil() to Math.floor() , because of ceil it takes a size of +1 than you need.

I made changes to your code, it will work here.

 function getIntervalls(max, nbIntervalls) { var size = Math.floor(max / nbIntervalls); var result = []; if (size > 0) { for (let i = 0; i < nbIntervalls; i++) { var inf = i + i * size; var sup = inf + size < max ? inf + size : max; result .push([inf, sup]); if (sup >= max) { break; } } } else { result.push([0, max]); } return result; } console.log(JSON.stringify(getIntervalls(10, 5))); 

Hope this helps!

+2
source

You can check if i zero for the first element, and if the next increment is greater than the maximum for the second element. You can also check if the first element is smaller than max.

 function getIntervalls(max, nInt) { const c = Math.floor(max / nInt); const r = []; for (var i = 0; i <= max; i += c) { const a = i == 0 ? i : i += 1; const b = i + c > max ? max : i + c; if (a < max) r.push([a, b]) } return r; } console.log(JSON.stringify(getIntervalls(3, 2))); console.log(JSON.stringify(getIntervalls(6, 2))); console.log(JSON.stringify(getIntervalls(8, 3))); console.log(JSON.stringify(getIntervalls(9, 3))); console.log(JSON.stringify(getIntervalls(7, 2))); console.log(JSON.stringify(getIntervalls(7, 3))); 
+1
source

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


All Articles