Thue-Morse sequence
Last Updated :
05 Apr, 2023
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++
#include <bits/stdc++.h>
using namespace std;
string complement(string s)
{
string comps;
for ( int i = 0; i < s.length(); i++) {
if (s.at(i) == '0' )
comps += '1' ;
else
comps += '0' ;
}
return comps;
}
string nthTerm( int n)
{
string s = "0" ;
for ( int i = 1; i < n; i++)
s += complement(s);
return s;
}
int main()
{
int n = 4;
cout << nthTerm(n) << endl;
return 0;
}
|
Java
class GFG
{
static String complement(String s)
{
String comps = "" ;
for ( int i = 0 ; i < s.length(); i++)
{
if (s.charAt(i) == '0' )
comps += '1' ;
else
comps += '0' ;
}
return comps;
}
static String nthTerm( int n)
{
String s = "0" ;
for ( int i = 1 ; i < n; i++)
s += complement(s);
return s;
}
public static void main(String[] args)
{
int n = 4 ;
System.out.print(nthTerm(n));
}
}
|
Python3
def complement(s):
comps = "";
for i in range ( len (s)):
if (s[i] = = '0' ):
comps + = '1' ;
else :
comps + = '0' ;
return comps;
def nthTerm(n):
s = "0" ;
for i in range ( 1 , n):
s + = complement(s);
return s;
n = 4 ;
print (nthTerm(n));
|
C#
using System;
class GFG
{
static string complement( string s)
{
string comps = "" ;
for ( int i = 0; i < s.Length; i++)
{
if (s[i] == '0' )
comps += '1' ;
else
comps += '0' ;
}
return comps;
}
static string nthTerm( int n)
{
string s = "0" ;
for ( int i = 1; i < n; i++)
s += complement(s);
return s;
}
static void Main()
{
int n = 4;
Console.Write(nthTerm(n));
}
}
|
PHP
<?php
function complement( $s )
{
$comps = "" ;
for ( $i = 0;
$i < strlen ( $s ); $i ++)
{
if ( $s [ $i ] == '0' )
$comps .= '1' ;
else
$comps .= '0' ;
}
return $comps ;
}
function nthTerm( $n )
{
$s = "0" ;
for ( $i = 1; $i < $n ; $i ++)
$s .= complement( $s );
return $s ;
}
$n = 4;
echo nthTerm( $n );
?>
|
Javascript
<script>
function complement(s)
{
let comps = "" ;
for (let i = 0; i < s.length; i++)
{
if (s[i] == '0' )
comps += '1' ;
else
comps += '0' ;
}
return comps;
}
function nthTerm(n)
{
let s = "0" ;
for (let i = 1; i < n; i++)
s += complement(s);
return s;
}
let n = 4;
document.write(nthTerm(n));
</script>
|
Time Complexity: O(n*log(n))
Auxiliary Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...