Intro
The Switching sequence is defined like so:
Start with n
people standing in a circle (6
for this example).
1 2
6 3
5 4
Starting from person 1
, the person that is to the left of the "chosen" person is removed.
1
6 3
5 4
The removed person can "switch" the removal method up:
- If the removed person is even (which it is in this case), the next removed person will be to the right of the next "chosen" person.
- If the removed person is odd, the next removed person will be to the left of the next "chosen" person.
The next chosen person is also dependent on the previously removed person.
- If the removed person is even, the next chosen person will be to the right of the previous chosen person.
- If the removed person is odd, see above, but replace "right" with "left".
So the next chosen person is then 6
.
Now we remove the person to the right of 6
, which is 5
:
1
6 3
4
Because 5
is odd, the removed person is now to the left. The new chosen person is 1
.
We now remove 3
:
1
6
4
We continue this process, until we're left with 1 number - in this example, the final number is 1
. So therefore S(6) = 1
.
The first few numbers are:
n | S(n)
---------
1 | 1
2 | 1
3 | 3
4 | 1
5 | 5
6 | 1
7 | 3
8 | 6
9 | 5
10 | 6
11 | 9
Task
Your task is to make a program (or a function) that returns S(n)
(the n
th number in the Switching sequence) when given n
, using the least amount of bytes.
Example inputs and outputs:
1 -> 1
10 -> 6
13 -> 13
You are guaranteed to get a positive integer.
This is code-golf, so shortest code in bytes wins!
Note: There is no OEIS sequence (what?), to save you the trouble of searching.
2
never remains, but does7
? \$\endgroup\$