lib/dist.js
function distribute(m, n) {
  m = Number(m);
  n = Number(n);

  let result = [];

  if (m < n) {
    for (let i = 0; i < n; i++) {
      result.push(i < m ? 1 : 0);
    }
  } else {
    let baseCount = Math.floor(m / n);
    let extraItems = m % n;
    for(let i = 0; i < n; i++) {
      result.push(baseCount);
      if (extraItems > 0) {
        result[i]++;
        extraItems--;
      }
    }
  }
  assert(m === sum(result), `${m} === ${sum(result)}`);
  return result;
}
function distribute(m, n) {
  m = Number(m);
  n = Number(n);

  let result = [];

  if (m < n) {
    for (let i = 0; i < n; i++) {
      result.push(i < m ? 1 : 0);
    }
  } else {
    let baseCount = Math.floor(m / n);
    let extraItems = m % n;
    for(let i = 0; i < n; i++) {
      result.push(baseCount);
      if (extraItems > 0) {
        result[i]++;
        extraItems--;
      }
    }
  }
  assert(m === sum(result), `${m} === ${sum(result)}`);
  return result;
}
function distribute ( m , n ) { m = Number ( m ) ; n = Number ( n ) ; let result = [ ] ; if ( m < n ) { for ( let i = 0 ; i < n ; i ++ ) { result . push ( i < m ? 1 : 0 ) ; } } else { let baseCount = Math . floor ( m / n ) ; let extraItems = m % n ; for ( let i = 0 ; i < n ; i ++ ) { result . push ( baseCount ) ; if ( extraItems > 0 ) { result [ i ] ++ ; extraItems -- ; } } } assert ( m === sum ( result ) , ` ${ m } ${ sum ( result ) } ` ) ; return result ; }
Given M "things", distribute them between N peers as equally as possible
Given M things distribute them between N peers as equally as possible