Part of Code Golf Advent Calendar 2022 event. See the linked meta post for details.
The Elves like playing number games. One day, Bin (a friend of Fen) suggests a new game: given a positive integer n
, each Elf says a number which has exactly n
runs of ones in binary. The first Elf must say the smallest number, and the next ones must say the smallest number greater than the last one.
Task
Given n
, output the sequence.
You can use any of sequence I/O methods:
- Given
n
, output the corresponding sequence infinitely - Given
n
and the indexk
(may be 1- or 0-indexed), output thek
th number in the sequence forn
- Given
n
andk
, output the firstk
numbers of the sequence forn
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
n = 1
1, 2, 3, 4, 6, 7, 8, 12, 14, 15, 16, 24, 28, 30, 31, 32, 48, 56, 60, 62, 63,
64, 96, 112, 120, 124, 126, 127, ...
n = 2
5, 9, 10, 11, 13, 17, 18, 19, 20, 22, 23, 25, 26, 27, 29, 33, 34, 35, 36, 38,
39, 40, 44, 46, 47, 49, 50, 51, 52, 54, 55, 57, 58, 59, 61, 65, 66, 67, 68, ...
n = 3
21, 37, 41, 42, 43, 45, 53, 69, 73, 74, 75, 77, 81, 82, 83, 84, 86, 87, 89,
90, 91, 93, 101, 105, 106, 107, 109, 117, ...
n = 4
85, 149, 165, 169, 170, 171, 173, 181, 213, 277, 293, 297, 298, 299, 301, 309,
325, 329, 330, 331, 333, 337, 338, 339, 340, 342, 343, 345, ...
n = 5
341, 597, 661, 677, 681, 682, 683, 685, 693, 725, 853, 1109, 1173, 1189, 1193,
1194, 1195, 1197, 1205, 1237, 1301, 1317, 1321, 1322, 1323, 1325, ...
n = 6
1365, 2389, 2645, 2709, 2725, 2729, 2730, 2731, 2733, 2741, 2773, 2901, 3413,
4437, 4693, 4757, 4773, 4777, 4778, 4779, 4781, 4789, 4821, 4949, 5205, ...
The corresponding OEIS sequences are A023758 (without leading 0), A043682, A043683, A043684, A043685, and A043686.