Note to self: make interactive version
constfunction generate() {
let n = 10;parseInt(document.getElementById("n").value);
const let k = 7;parseInt(document.getElementById("k").value);
var c = [null]; // JavaScript arrays are 0-indexed
var j, output = [];
var skipToR4 = false, skipToR5 = false; // to simulate gotos
// R1
for (var i = 1; i <= k; i++) {
c.push(i - 1);
}
c.push(n);
while (true) {
if (!skipToR4) {
// R2
output.push("{" + c.slice(1, k + 1).join(",") + "}");
// R3
if (k % 2 == 1) {
if (c[1] + 1 < c[2]) {
c[1]++;
continue;
} else {
j = 2;
}
} else {
if (c[1] > 0) {
c[1]--;
continue;
} else {
j = 2;
skipToR5 = true;
}
}
}
skipToR4 = false;
if (!skipToR5) {
// R4
if (c[j] >= j) { // try to decrease c[j]
c[j] = c[j - 1];
c[j - 1] = j - 2;
continue;
} else {
j++;
}
}
skipToR5 = false;
// R5
if (c[j] + 1 < c[j + 1]) { // try to increase c[j]
c[j - 1] = c[j];
c[j]++;
continue;
} else {
j++;
if (j <= k) {
skipToR4 = true;
} else {
break;
}
}
}
console document.loggetElementById("output").innerText = output.join("\n"));
}
<label for="n">n:</label><input id="n" type="number" value="5"><br/>
<label for="k">k:</label><input id="k" type="number" value="3"><br/>
<button onclick="generate();">Generate</button><br/>
<span>Output:</span>
<pre id="output"></pre>