August 17, 2020
The Baum–Sweet sequence is an infinite automatic sequence of
0s and1s defined by the rule:
bn =
1if the binary representation ofncontains no block of consecutive0s of odd length; bn =0otherwise;for
n ≥ 0.Define a generator function
baumSweetthat sequentially generates the values of this sequence.
const getBaumSweet = (n) => {
let binaryList = [];
let res = 0;
while (n !== 1) {
binaryList.push(n % 2);
n = Math.floor(n / 2);
}
binaryList.push(n);
binaryList.reverse();
for (let i = 0; i < binaryList.length - 1; i++) {
switch (binaryList[i]) {
case 0:
res++;
break;
case 1:
if (res === 1) break;
}
if (res % 2 === 0) res = 0;
}
return res === 0;
};