Task

Create a Thue-Morse sequence.



See also
   YouTube entry: The Fairest Sharing Sequence Ever
   YouTube entry: Math and OCD - My story with the Thue-Morse sequence
   Task: Fairshare between two and more


For x = 0 to 6                         ' sequence loop
  Print Using "_#";x;": ";             ' print sequence
  For y = 0 To (2^x)-1                 ' element loop
    Print AND(FUNC(_Parity(y)),1);     ' print element
  Next                                 ' next element
  Print                                ' terminate elements line
Next                                   ' next sequence
 
End
 
_Parity Param (1)                      ' parity function
  Local (1)                            ' number of bits set
  b@ = 0                               ' no bits set yet
  Do While a@ # 0                      ' until all bits are counted
    If AND (a@, 1) Then b@ = b@ + 1    ' bit set? increment count
    a@ = SHL(a@, -1)                   ' shift the number
  Loop
Return (b@)                            ' return number of bits set