Find nth term of the Dragon Curve Sequence
Dragon Curve Sequence is an infinite binary sequence of 0’s and 1’s. The first term of the sequence is 1.
From the next term, we alternately insert 1 and 0 between each element of the previous term.
To understand better refer the following explanations:
- 1 (starts with 1)
- “1” 1 “0”
1 and 0 are inserted alternately to the left and right of the previous term. Here the number in the double quotes represents the newly added elements.
So the second term becomes
1 1 0 - “1” 1 “0” 1 “1” 0 “0”
So the third term becomes
1 1 0 1 1 0 0
- “1” 1 “0” 1 “1” 0 “0” 1 “1” 1 “0” 0 “1” 0 “0”
The fourth term becomes
1 1 0 1 1 0 0 1 1 1 0 0 1 0 0
This is also popularly known as the regular paperfolding sequence. Given a natural number n, the task is to find the nth string formed by Dragon Curve sequence of length .
Examples:
Input: n = 4
Output: 110110011100100
Explanation:
We get 1 as the first term,
"110" as the second term,
"1101100" as the third term ,
And hence our fourth term will be
"110110011100100"
Input: n = 3
Output: 1101100
Approach:
- Step 1: Start with the first term 1. Then add 1 and 0 alternately after each element of the preceding term.
- Step 2: The new term obtained becomes the current term.
- Step 3: Repeat the process in a loop from 1 to n, to generate each term and finally the nth term.
Algorithm :
- Step 1: Take the input size n
- Step 2: Initialize 1st term of string as “1”.
- step 3: generate each term of the sequence using nested for loop.
- Step 4: Add alternate 0 and 1 in between, if previous term is 1 then add 0; vice versa.
- Step 5: Print the output string.
Below is the implementation of above idea:
C++
#include <bits/stdc++.h>
using namespace std;
string Dragon_Curve_Sequence( int n)
{
string s = "1" ;
for ( int i = 2; i <= n; i++)
{
string temp = "1" ;
char prev = '1' , zero = '0' , one = '1' ;
for ( int j = 0; j < s.length(); j++)
{
temp += s[j];
if (prev == '0' )
{
temp += one;
prev = one;
}
else
{
temp += zero;
prev = zero;
}
}
s = temp;
}
return s;
}
int main()
{
int n = 4;
string s = Dragon_Curve_Sequence(n);
cout << s << "\n" ;
}
|
Java
import java.util.*;
class solution
{
static String Dragon_Curve_Sequence( int n)
{
String s = "1" ;
for ( int i = 2 ; i <= n; i++)
{
String temp = "1" ;
char prev = '1' , zero = '0' , one = '1' ;
for ( int j = 0 ; j < s.length(); j++)
{
temp += s.charAt(j);
if (prev == '0' )
{
temp += one;
prev = one;
}
else
{
temp += zero;
prev = zero;
}
}
s = temp;
}
return s;
}
public static void main(String args[])
{
int n = 4 ;
String s = Dragon_Curve_Sequence(n);
System.out.println(s);
}
}
|
Python
def Dragon_Curve_Sequence(n):
s = "1"
for i in range ( 2 , n + 1 ):
temp = "1"
prev = '1'
zero = '0'
one = '1'
for j in range ( len (s)):
temp + = s[j]
if (prev = = '0' ):
temp + = one
prev = one
else :
temp + = zero
prev = zero
s = temp
return s
n = 4
s = Dragon_Curve_Sequence(n)
print (s)
|
C#
using System;
class GFG
{
static String Dragon_Curve_Sequence( int n)
{
String s = "1" ;
for ( int i = 2; i <= n; i++)
{
String temp = "1" ;
char prev = '1' , zero = '0' , one = '1' ;
for ( int j = 0; j < s.Length; j++)
{
temp += s[j];
if (prev == '0' )
{
temp += one;
prev = one;
}
else
{
temp += zero;
prev = zero;
}
}
s = temp;
}
return s;
}
public static void Main()
{
int n = 4;
String s = Dragon_Curve_Sequence(n);
Console.WriteLine(s);
}
}
|
PHP
<?php
function Dragon_Curve_Sequence( $n )
{
$s = "1" ;
for ( $i = 2; $i <= $n ; $i ++)
{
$temp = "1" ;
$prev = '1' ;
$zero = '0' ;
$one = '1' ;
for ( $j = 0; $j < strlen ( $s ); $j ++)
{
$temp .= $s [ $j ];
if ( $prev == '0' )
{
$temp .= $one ;
$prev = $one ;
}
else
{
$temp .= $zero ;
$prev = $zero ;
}
}
$s = $temp ;
}
return $s ;
}
$n = 4;
$s = Dragon_Curve_Sequence( $n );
echo $s . "\n" ;
?>
|
Javascript
<script>
function Dragon_Curve_Sequence(n)
{
let s = "1" ;
for (let i = 2; i <= n; i++)
{
let temp = "1" ;
let prev = '1' ;
let zero = '0' ;
let one = '1' ;
for (let j = 0; j < s.length; j++)
{
temp = temp + s[j];
if (prev == '0' )
{
temp += one;
prev = one;
}
else
{
temp += zero;
prev = zero;
}
}
s = temp;
}
return s;
}
let n = 4;
let s = Dragon_Curve_Sequence(n);
document.write(s + "<br>" );
</script>
|
Output:
110110011100100
Complexity Analysis:
- Time Complexity: O(n*s) where s is the length of resultant string
- Auxiliary Space: O(s) where s is the length of resultant string
Last Updated :
19 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...