Thue-Morse sequence
Thue–Morse sequence, or Prouhet–Thue–Morse sequence, is an infinite binary sequence of 0s and 1s. The sequence is obtained by starting with 0 and successively appending the Boolean complement of the sequence obtained so far.
First few steps :
Start with 0
Append complement of 0, we get 01
Append complement of 01, we get 0110
Append complement of 0110, we get 01101001
Given a whole number n. The task is to find the nth string formed of by Thue–Morse sequence i.e prefix of length 2n-1 of Thue–Morse sequence.
Examples:
Input : n = 4 Output : 01101001 We get 0, 01, 0110 and 01101001 in fourth iteration. Input : n = 3 Output : 0110
The idea is to initialize the output string with 0, then run a loop n – 1 times and for each iteration find the complement of the string and append it to the string.
Below is implementation of this approach:
C++
// CPP Program to find nth term of Thue-Morse sequence. #include <bits/stdc++.h> using namespace std; // Return the complement of the binary string. string complement(string s) { string comps; // finding the complement of the string. for (int i = 0; i < s.length(); i++) { // if character is 0, append 1 if (s.at(i) == '0') comps += '1'; // if character is 1, append 0. else comps += '0'; } return comps; } // Return the nth term of Thue-Morse sequence. string nthTerm(int n) { // Initialing the string to 0 string s = "0"; // Running the loop for n - 1 time. for (int i = 1; i < n; i++) // appending the complement of // the string to the string. s += complement(s); return s; } // Driven Program int main() { int n = 4; cout << nthTerm(n) << endl; return 0; } |
Java
// Java Program to find nth // term of Thue-Morse sequence. class GFG { // Return the complement // of the binary String. static String complement(String s) { String comps = ""; // finding the complement // of the String. for (int i = 0; i < s.length(); i++) { // if character is 0, // append 1 if (s.charAt(i) == '0') comps += '1'; // if character is 1, // append 0. else comps += '0'; } return comps; } // Return the nth term // of Thue-Morse sequence. static String nthTerm(int n) { // Initialing the // String to 0 String s = "0"; // Running the loop // for n - 1 time. for (int i = 1; i < n; i++) // appending the complement of // the String to the String. s += complement(s); return s; } // Driven Code public static void main(String[] args) { int n = 4; System.out.print(nthTerm(n)); } } // This code is contributed by // mits |
Python3
# Python3 Program to find nth term of # Thue-Morse sequence. # Return the complement of the # binary string. def complement(s): comps = ""; # finding the complement # of the string. for i in range(len(s)): # if character is 0, append 1 if (s[i] == '0'): comps += '1'; # if character is 1, append 0. else: comps += '0'; return comps; # Return the nth term of # Thue-Morse sequence. def nthTerm(n): # Initialing the string to 0 s = "0"; # Running the loop for n - 1 time. for i in range(1, n): # appending the complement of # the string to the string. s += complement(s); return s; # Driver Code n = 4; print(nthTerm(n)); # This code is contributed # by mits |
C#
// C# Program to find nth // term of Thue-Morse sequence. using System; class GFG { // Return the complement // of the binary string. static string complement(string s) { string comps = ""; // finding the complement // of the string. for (int i = 0; i < s.Length; i++) { // if character is 0, // append 1 if (s[i] == '0') comps += '1'; // if character is 1, // append 0. else comps += '0'; } return comps; } // Return the nth term // of Thue-Morse sequence. static string nthTerm(int n) { // Initialing the // string to 0 string s = "0"; // Running the loop // for n - 1 time. for (int i = 1; i < n; i++) // appending the complement of // the string to the string. s += complement(s); return s; } // Driven Code static void Main() { int n = 4; Console.Write(nthTerm(n)); } } // This code is contributed by // Manish Shaw(manishshaw1) |
PHP
<?php // PHP Program to find nth // term of Thue-Morse sequence. // Return the complement // of the binary string. function complement($s) { $comps = ""; // finding the complement // of the string. for ($i = 0; $i < strlen($s); $i++) { // if character is // 0, append 1 if ($s[$i] == '0') $comps .= '1'; // if character is // 1, append 0. else $comps .= '0'; } return $comps; } // Return the nth term // of Thue-Morse sequence. function nthTerm($n) { // Initialing the // string to 0 $s = "0"; // Running the loop // for n - 1 time. for ($i = 1; $i < $n; $i++) // appending the complement of // the string to the string. $s .= complement($s); return $s; } // Driven Code $n = 4; echo nthTerm($n); // This code is contributed // by mits ?> |
Output:
01101001
Recommended Posts:
- Convert an unbalanced bracket sequence to a balanced sequence
- Sum of the sequence 2, 22, 222, .........
- Look-and-Say Sequence
- k-th number in the Odd-Even sequence
- Aronson's Sequence
- Sylvester's sequence
- Alcuin's Sequence
- Gijswijt's Sequence
- Connell Sequence
- Aliquot Sequence
- Recaman's sequence
- Padovan Sequence
- Juggler Sequence
- Find F(n) when F(i) and F(j) of a sequence are given
- Farey Sequence
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



