Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Thue–Morse sequence
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Fast sequence generation === This method leads to a fast method for computing the Thue–Morse sequence: start with {{math|1=''t''<sub>0</sub> = 0}}, and then, for each ''n'', find the highest-order bit in the binary representation of ''n'' that is different from the same bit in the representation of {{math|1=''n'' − 1}}. If this bit is at an even index, ''t<sub>n</sub>'' differs from {{math|1=''t''<sub>''n'' − 1</sub>}}, and otherwise it is the same as {{math|1=''t''<sub>''n'' − 1</sub>}}. In [[Python (programming language)|Python]]: <syntaxhighlight lang="python"> def generate_sequence(seq_length: int) -> int: """Thue–Morse sequence.""" value: int = 1 for n in range(seq_length): # Note: assumes that (-1).bit_length() gives 1 x: int = (n ^ (n - 1)).bit_length() + 1 if x & 1 == 0: # Bit index is even, so toggle value value = 1 - value yield value </syntaxhighlight> The resulting algorithm takes constant time to generate each sequence element, using only a [[L (complexity)|logarithmic number of bits]] (constant number of words) of memory.{{sfnp|Arndt|2011}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)