[algorithm] Algorithm to return all combinations of k elements from n

Here is a simple JS solution:

_x000D_
_x000D_
function getAllCombinations(n, k, f1) {_x000D_
 indexes = Array(k);_x000D_
  for (let i =0; i< k; i++) {_x000D_
   indexes[i] = i;_x000D_
  }_x000D_
  var total = 1;_x000D_
  f1(indexes);_x000D_
  while (indexes[0] !== n-k) {_x000D_
   total++;_x000D_
  getNext(n, indexes);_x000D_
    f1(indexes);_x000D_
  }_x000D_
  return {total};_x000D_
}_x000D_
_x000D_
function getNext(n, vec) {_x000D_
 const k = vec.length;_x000D_
  vec[k-1]++;_x000D_
 for (var i=0; i<k; i++) {_x000D_
   var currentIndex = k-i-1;_x000D_
    if (vec[currentIndex] === n - i) {_x000D_
    var nextIndex = k-i-2;_x000D_
      vec[nextIndex]++;_x000D_
      vec[currentIndex] = vec[nextIndex] + 1;_x000D_
    }_x000D_
  }_x000D_
_x000D_
 for (var i=1; i<k; i++) {_x000D_
    if (vec[i] === n - (k-i - 1)) {_x000D_
      vec[i] = vec[i-1] + 1;_x000D_
    }_x000D_
  }_x000D_
 return vec;_x000D_
} _x000D_
_x000D_
_x000D_
_x000D_
let start = new Date();_x000D_
let result = getAllCombinations(10, 3, indexes => console.log(indexes)); _x000D_
let runTime = new Date() - start; _x000D_
_x000D_
console.log({_x000D_
result, runTime_x000D_
});
_x000D_
_x000D_
_x000D_