Introduction
The ruler sequence is the sequence of the largest possible numbers \$a_n\$ such that \$2^{a_n}\mid n\$. It is so-called because its pin plot looks similar to a ruler's markings:
However, with a slight modification, we can also get a similar sequence. This sequence is \$\{a_1,a_2,a_3,…\}\$ where \$a_n\$ is the largest power of 2 such that \$a_n\mid n\$ (relevant OEIS).
The first 100 terms of this sequence are:
1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 32, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 64, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 32, 1, 2, 1, 4
Challenge
Your challenge is to do one of these three things:
- Take a positive integer \$n\$ as input and return the \$n\$th term of this sequence.
- Take a positive integer \$n\$ as input and return the first \$n\$ terms of this sequence.
- Output the sequence infinitely.
Test Cases
12 -> 4
64 -> 64
93 -> 1
8 -> 8
0 -> (undefined behavior)
Rules
- You may output the sequence in any convenient format - e.g. as a list or some other iterable or separated by any non-digit separator, as long as it is constant between all terms.
- You may output the term(s) in any convenient format - e.g. as an integer, as an integer string, as a float with the decimal part consisting of only zeros, ditto but as a string, or as a Boolean (
True
) if and only if the term is1
. - You may choose to use either zero- or one-indexing.
- Standard loopholes are forbidden.
- Trailing whitespace is allowed.
- If possible, please link to an online interpreter (e.g. TIO) to run your program on.
- Please explain your answer. This is not necessary, but it makes it easier for others to understand.
- This is code-golf, so shortest code in bytes wins!
o
raised to the power ofk
(index) of the first1
from theR
ight in the implicit input inb
inary \$\endgroup\$