8
Introduction - What is a necklace?
A necklace is something that OEIS people are obsessed with. The OEIS challenge has like 5 necklace sequences.
A binary necklace of length n
is a loop with n
beads that are either 0
or 1
. Two necklaces are the same if one can be rotated to become the other, and two reversible necklaces are the same if one can be rotated, reversed, or reversed and rotated to become the other.
A primitive necklace is one that cannot be expressed as more than one copy of a string of beads chained together. Note that the copies must be assembled all in the same order (no reversing) for a necklace to be considered non-primitive.
For example, let's take a look at this necklace: 0 1 1 0 1 1
. It is not primitive because it can be expressed as 0 1 1
repeated twice. 0 1 0 1 1
is primitive.
0 1 1 0
is primitive because 0 1
and 1 0
are not considered the same string. This necklace is equivalent to 1 1 0 0
because it can be rotated left one bead to become this one, but not equivalent to 0 1 0 1
(which isn't primitive by the way).
Challenge
Given an non-negative integer n
, return the number of distinct reversible primitive binary necklaces of length n
. Input and output as a single integer each.
The first few terms of this sequence are 1, 2, 1, 2, 3, 6, 8, 16, 24, 42, 69, 124, 208, 378, 668, 1214, 2220, 4110
, 0-indexed.
This is OEIS A001371
Reference Implementation in Python 3 - quite slow
4Oh, I get it—"primitive" means it can't be partially rotated to get the original necklace, right? – ETHproductions – 2017-09-24T01:41:36.100
@ETHproductions ... I never thought of it that way. Yes, that is definitely a correct understanding. Nice! – HyperNeutrino – 2017-09-24T01:42:31.383
Why is
0 1 0 1
primitive? Isn't it0 1
repeated twice? – Misha Lavrov – 2017-09-24T01:49:29.427@MishaLavrov Thanks; I meant "not primitive" whoops. Thanks – HyperNeutrino – 2017-09-24T01:51:40.177
ಠಠ I was looking for an OEIS sequence to post as a challenge and the next thing I see is... this... necklaces. .. – totallyhuman – 2017-09-24T03:06:58.397
@icrieverytim see I had to suffer the first necklace question with unknown terms that almost killed the challenge, so time for revenge mwahahaha – HyperNeutrino – 2017-09-24T03:08:08.473
Trying to do this manually for n = 7 before I try to code it, and coming up two short. A string of all 0's or all 1's is not primitive right? That said, I do get 16 if I count those strings that cannot be reversed to get themselves. – Tahg – 2017-09-24T09:39:49.637
@Tahg The sixteen I get are:
0000001
,0000011
,0000101
,0000111
,0001001
,0001011
,0001111
,0010011
,0010101
,0010111
,0011011
,0011111
,0101011
,0101111
,0110111
,0111111
– Misha Lavrov – 2017-09-24T15:47:41.900@HyperNeutrino, what's your definition of "almost killed the challenge"? By my criteria you've saved the chain two times but they had nothing to do with necklaces.
– Peter Taylor – 2017-09-24T16:11:02.987@PeterTaylor I guess "killed the challenge" wouldn't be the right way to word it; something like the first one that involved weird math that needed Math.SE to complete (but even then I'm not sure it was the first lol... hm) – HyperNeutrino – 2017-09-24T16:56:37.820
Ok, it seems like I was just confused on the definition of a reversible primitive. I had in mind a number that, when reversed, could be rotated to be itself, but rereading the question doesn't seem to imply that at all. In the case of n = 7, 14/16 had this property of symmetry, though I couldn't find an OEIS that matched that. – Tahg – 2017-09-24T17:06:14.580