Building a Logic!

What's Logic Behind this sequence 1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211.... !?
I have to print it using loops. But not comming up with logic ?

Can someone Just help me.....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

string getNextTerm( const string &str )
{
   int num = 1;
   while ( num < str.size() && str[num] == str[0] ) num++;
   return to_string( num ) + str[0] + ( num < str.size() ? getNextTerm( str.substr( num ) ) : "" );
}

int main()
{
   for ( string s = "1"; s.size() < 200; s = getNextTerm( s ) ) cout << s << '\n';
}
1 11 101 111011 11110101 100110111011
Yes, @Ganado!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;

string toBinary( int num ) { return ( num > 1 ? toBinary( num / 2 ): "" ) + to_string( num % 2 ); }

string getNextTerm( const string &str )
{
   int num = 1;
   while ( num < str.size() && str[num] == str[0] ) num++;
   return toBinary( num ) + str[0] + ( num < str.size() ? getNextTerm( str.substr( num ) ) : "" );
}

int main()
{
   for ( string s = "1"; s.size() < 200; s = getNextTerm( s ) ) cout << s << '\n';
}
Last edited on
Topic archived. No new replies allowed.