14
Definition
From the description on OEIS A006345:
To find
a(n), consider either a1or a2. For each, find the longest repeated suffix, that is, for each ofa(n)=1,2, find the longest sequenceswith the property that the sequencea(1),...,a(n)ends withss. Use the digit that results in the shorter such suffix.a(1) = 1.
Worked-out Example
a(1)=1.
If a(2)=1, we will have the sequence 1 1 where the longest doubled substring from the end is 1. If a(2)=2 instead, then it would be the empty substring. Therefore a(2)=2.
When n=6, we choose between 1 2 1 1 2 1 and 1 2 1 1 2 2. In the first choice, 1 2 1 is doubled consecutively from the end. In the second choice, it is 2 instead. Therefore, a(6)=2.
When n=9, we choose between 1 2 1 1 2 2 1 2 1 and 1 2 1 1 2 2 1 2 2. In the first choice, the longest doubled consecutive substring is 2 1, while in the second choice 1 2 2 is doubled consecutively at the end. Therefore a(9)=1.
Task
Given n, return a(n).
Specs
nwill be positive.- You can use 0-indexed instead of 1-indexed. In that case, please state so in your answer. Also, in that case,
ncan be0also.
Testcases
The testcases are 1-indexed. However, you can use 0-indexed.
n a(n)
1 1
2 2
3 1
4 1
5 2
6 2
7 1
8 2
9 1
10 1
11 2
12 1
13 2
14 2
15 1
16 1
17 2
18 1
19 1
20 1
References
- WolframMathWorld
- Obligatory OEIS A006345
1In the test case of
n=9, the first choice1 2 1 1 2 2 1 2 1has the doubled substring2 1at the end. – Sherlock9 – 2016-08-01T19:02:09.5601Note that the linked OEIS page has a golfed Perl solution of ~43 bytes. – liori – 2016-08-02T01:21:16.250