# Series Puzzle

• 03-05-2010
Rajan_singh
Series Puzzle
HI

I have been scratching my head for the following series problem since infinity but coudnt find a solution to this..can any one plz help me with the logic..

121 12321 1234321 123454321

We need to print this without using if and only single for statement...
• 03-05-2010
laserlight
What exactly is the problem? After all, if you are only supposed to print exactly that, you do not even need a for loop at all ;)
• 03-05-2010
Rajan_singh
Sowiee for not explaining my problem properly.Actually,we need to use exactly one "for" loop nothing more than that nothing less.I hope im being more clear dis time
• 03-05-2010
laserlight
That is not too difficult: if you compute the difference between consecutive terms, you would notice a pattern that looks like it could be exploited. The problem is, this requires computation, which limits the number of terms considerably since the next term in the sequence would might not be within the range of the standard built-in integer types. (Consequently, it would be simpler to just print "121 12321 1234321 123454321", or store the terms in an array then print the elements of the array.)

Doing this by treating the terms as strings instead of numbers is also easy, except that the artificial restrictions of this trivia question makes it difficult.
• 03-05-2010
Rajan_singh
Thnks for the help.I will try and post the solution soon.. :-)
• 03-05-2010
anon
I suppose one way would be to figure out a way to generate those numbers mathematically.

For example, you can observe that 1 + 120 = 121, 121 + 12200 = 12321, 12321 + 1222000 = 1234321 etc.

Now you can determine the regularity and to figure out how to make those calculations in the loop.

(I suppose you might be expected to use math, since you could easily overflow 32-bit integers if you took the sequence further.)
• 03-05-2010
laserlight
Bah, I have solved it using a std::stringstream and a std::string. Basically, I divide the output string into two parts. The left part "grows" rightwards (and thus I use a stringstream), whereas the right part "grows" leftwards (and thus I use a string, but with a stringstream to perform the appending in the loop).

It is probably rather inefficient though, but then this is a silly trivia question.
• 03-05-2010
rogster001
why not just use a recursive counting function that takes the current highest value as an argument, count up to it (print individual number out each time) then back down to the minimum, increment the max value, call function

need an escape condition too of course, or overflow quite fast methinks
• 03-05-2010
_Mike
Well if the only requirements are no if's and must use a for loop then why not just
Code:

```for(int i=0; i<1; ++i)     std::cout << "121 12321 1234321 123454321" << std::endl;```
:D
• 03-05-2010
anon
Yes, quite simple with string manipulation too.

For example, take the string apart into substrings and concatenate with a character added inbetween. Or keep inserting the new character and the old middle character.

However, using strings means using hidden loops and ifs.

Quote:

need an escape condition too of course, or overflow quite fast methinks
Wouldn't that basically mean an if?
• 03-05-2010
laserlight
Quote:

Originally Posted by anon
However, using strings means using hidden loops and ifs.

That is true. On the other hand, using standard I/O also means hidden loops and ifs, thus the problem may be impossible to solve when interpreted in this way.

Quote:

Originally Posted by anon
Wouldn't that basically mean an if?

I guess that you can use a for loop that runs exactly once if the condition is true. But yeah, I believe most of our replies are actually subverting this trivia question, which sounds like it might be a homework question.
• 03-05-2010
rogster001
Quote:

Wouldn't that basically mean an if?
erm...yes.. i will get my coat,

you could however question how many times this loop has to run, and use the for loop parameters with a division expression or something to provide the escape,

edit : i see already posted about how many times to loop...
• 03-05-2010
phantomotap
Do you have a "big number" library?

Soma

Code:

```#include <iostream> int main() {         for(unsigned long long x(11); 1111111111 != x; x = ((x * 10) + 1))         {                 std::cout << x * x << '\n';         }         return(0); }```