Series Puzzle

This is a discussion on Series Puzzle within the C++ Programming forums, part of the General Programming Boards category; HI I have been scratching my head for the following series problem since infinity but coudnt find a solution to ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Question 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...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    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

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Thnks for the help.I will try and post the solution soon.. :-)

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    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

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    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;

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    need an escape condition too of course, or overflow quite fast methinks
    Wouldn't that basically mean an if?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    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...

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,262
    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);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 15 puzzle help
    By rhodesianhunter in forum C Programming
    Replies: 14
    Last Post: 10-09-2009, 07:22 AM
  2. Replies: 12
    Last Post: 06-06-2008, 05:26 PM
  3. Crossword Puzzle Program
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2006, 11:08 PM
  4. Solution to Google Puzzle 3,3,8,8=24
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-01-2006, 09:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21