diff --git a/Exercises/1-ids.js b/Exercises/1-ids.js index 29b9450..6e91d57 100644 --- a/Exercises/1-ids.js +++ b/Exercises/1-ids.js @@ -1,20 +1,15 @@ 'use strict'; -const ids = function* () { - const free = ['0']; - const prepared = { has: false, value: '' }; - - while (true) { - if (prepared.has) { - prepared.has = false; - yield prepared.value; +const ids = function* (i = 0) { + for (;;++i) { + const left = 4 << i * 2; + for (let j = 0; j < 2 << i; j++) { + let res = 0; + for (let k = 0; k < i + 1; k++) { + res += (j & (1 << k)) << (k + 1); + } + yield (left + res).toString(2); } - const nextFree = free.shift(); - free.push('01' + nextFree); - free.push('00' + nextFree); - prepared.value = '11' + nextFree; - prepared.has = true; - yield '10' + nextFree; } };