Generate m-sequences
Haskell, 88 82 bytes
r@(h:t)%m=h:(t++[mod(sum[v|(v,1)<-zip r m])2])%m
l#x=take(2^l-1)$(1:(0<$[2..l]))%x
I'm using input method 1 (but in reverse order, because I'm shifting to the left) together with L
as an explicit parameter.
Usage example (test case #2): 4 # [1,1,0,0]
-> [1,0,0,0,1,0,0,1,1,0,1,0,1,1,1]
.
How it works: %
does the work. First parameter is the content of the shift register, second parameter is the feedback configuration. #
starts the whole process by building the initial register content, calling %
and taking the first 2^L-1
elements.
main function #:
1:0<$[2..l] -- initial register content: one 1 followed
-- by L-1 0s
%x -- call the helper function with the
-- feedback configuration
take(2^l-1) -- and take the first 2^L-1 elements
helper function % (bind r to the whole register set, h to the first register a0
and t to register a1..aL. Bind m to the feedback config):
h: -- next element is h, followed by
%m -- a recursive call of %, where the new register is
t++ -- t followed by
[v|(v,1)<-zip r m] -- the elements of l, where the corresponding
-- elements of m are "1"
mod(sum ... )2 -- xored
Pyth, 19 bytes
eM.u+xF*VQNPN+m0tQ1
Try it online: Demonstration or Test Suite
Explanation:
eM.u+xF*VQNPN+m0tQ1 implicit: Q = input list in format 1
m0tQ create a list with len(Q)-1 zeros
+ 1 and append a one at the end
this is the starting sequence
.u apply the following statement repeatedly to the starting seq. N:
*VQN vectorized multiplication of Q and N
xF xor all this values
+ PN remove the last value of N and prepend the new calculated one
.u stops, when it detects a cycle and returns a list of
all intermediate states of N
eM take the last element of each state and print this list
Jelly, 18 bytes
æḟ2µ&³B^/+ḤµḤ¡BḣḤṖ
This takes a single integer, encoding a0 in its MSB and aL-1 in its LSB.
The test cases become 1, 12, 18 and 48 in this format. Try it online!
How it works
æḟ2µ&³B^/+ḤµḤ¡BḣḤṖ Main link. Argument: n (binary-encoded taps)
æḟ2 Round n down to k = 2 ** (L - 1), the nearest power of 2.
µ Begin a new, monadic link. Argument: r = k
&³ Bitwise AND r with n.
B Convert to binary.
^/ Bitwise XOR all binary digits.
Ḥ Unhalve; yield 2r.
+ Add the results to left and right.
µ Convert the previous chain into a single link.
Ḥ Unhalve; yield 2k = 2 ** L.
¡ Execute the link 2k times, updating r in each iteration.
B Convert the final value of r to binary.
ḣḤ Keep only the first 2k binary digits.
Ṗ Pop; discard the last digit.