The Rudin-Shapiro sequence is a sequence of \$1\$s and \$-1\$s defined as follows: \$r_n = (-1)^{u_n}\$, where \$u_n\$ is the number of occurrences of (possibly overlapping) \$11\$ in the binary representation of \$n\$.
For example, \$r_{461} = -1\$, because \$461\$ in binary is \$111001101\$, which contains \$3\$ occurrences of \$11\$: \$\color{red}{\underline{11}}1001101\$, \$1\color{red}{\underline{11}}001101\$, \$11100\color{red}{\underline{11}}01\$.
This is sequence A020985 in the OEIS.
The first few terms of the sequence are:
1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1
Task
Generate the Rudin-Shapiro sequence.
As with standard sequence challenges, you may choose to:
- Take an integer \$n\$ as input and output the \$n\$th term of the sequence.
- Take an integer \$n\$ as input and output the first \$n\$ terms of the sequence.
- Take no input and output the sequence indefinitely.
This is code-golf, so the shortest code in bytes in each language wins.
Test cases
0 -> 1
1 -> 1
2 -> 1
3 -> -1
4 -> 1
5 -> 1
6 -> -1
7 -> 1
8 -> 1
9 -> 1
10 -> 1
11 -> -1
12 -> -1
13 -> -1
14 -> 1
15 -> -1
16 -> 1
17 -> 1
18 -> 1
19 -> -1