
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find nth term of the Dragon Curve Sequence in C++
Here we will see a program, that can find nth term of the Dragon Curve sequence. The Dragon curve sequence is an infinite binary sequence. It starts with 1, and in each step, it alternatively adds 1s and 0s before and after each element of the previous term, to form the next term.
- Term 1 : 1
- Term 2 : 110
- Term 3 : 1101100
- Term 4 : 110110011100100
We will start with 1, then add 1 and 0, alternatively after each element of the preceding term. When the new term obtained becomes the current term, then repeat the steps from 1 to n to generate next terms.
Example
#include <iostream> using namespace std; string dragCurveTerm(int n) { string term = "1"; for (int i = 2; i <= n; i++) { string temp = "1"; char prev = '1', zero = '0', one = '1'; for (int j = 0; j < term.length(); j++) { temp += term[j]; //take character from original string if (prev == '0') { temp += one; prev = one; } else { temp += zero; prev = zero; } } term = temp; } return term; } int main() { cout << "4th term of Dragon Curve Sequence: " << dragCurveTerm(4); }
Output
4th term of Dragon Curve Sequence: 110110011100100
- Related Articles
- The nth term of a sequence is $(2n-3)$, find its fifteenth term.
- Program to find nth term in Look and Say Sequence in Python
- Find the sum of first 20 terms of the sequence whose nth term is $a_n = An + B$.
- Consider the sequence 3, 6, 9, 12, 15, ..(1) Write down the next two terms of the sequence.(ii) Find, in terms of n, a formula for the nth term of the sequence.(iii) Hence, find the 105th term.
- Program to find nth term of a sequence which are divisible by a, b, c in Python
- Program to find nth sequence after following the given string sequence rules in Python
- Find the Nth term of the series 9, 45, 243,1377…in C++
- Find nth term of a given recurrence relation in Python
- Find nth term of a given recurrence relation in C++
- Program to find nth Fibonacci term in Python
- If the nth term of the A.P. $9, 7, 5, …$ is same as the nth term of the A.P. $15, 12, 9, …$ find $n$.
- Write the sequence with nth term:$a_n=3+4n$Show that all of the above sequences form A.P.
- Write the sequence with nth term:$a_n=5+2n$Show that all of the above sequences form A.P.
- Write the sequence with nth term:$a_n=6-n$Show that all of the above sequences form A.P.
- Write the sequence with nth term:$a_n=9-5n$Show that all of the above sequences form A.P.

Advertisements