3
\$\begingroup\$

I just finished my first homework for university where I had to create code that would generate all 6 digit palindrome numbers.

Can someone who has more knowledge give me some tips where I can improve my code?

#include <iostream>
#include <algorithm>
#include <sstream>

using namespace std;

int main(int argc, char* argv[] )
{
  string palindroms;

  for (int iter = 100000; iter <= 1000000; ++iter ) {
    stringstream ss;
    ss << iter;
    palindroms = ss.str();

    string RevStr = palindroms;
    reverse( RevStr.begin(), RevStr.end() );

    if ( RevStr == palindroms ) {
      cout << palindroms << endl;
    }
  }
return 0;
}
\$\endgroup\$

migrated from stackoverflow.com Oct 14 '17 at 13:24

This question came from our site for professional and enthusiast programmers.

  • \$\begingroup\$ my suggestion to you is to think like that : since you know that you want only palindrome so try not to loop over all the element and test if they are palindrome or not. only show palindromes ... and for a palindrom string, if you know the first half you have the second half. So you can easily create all the palindromes \$\endgroup\$ – Temani Afif Oct 13 '17 at 21:19
  • \$\begingroup\$ Your program tries every one of 900000 potential numbers and tests if it's a palindrome in a rather inefficent way. There should be exactly 900 palindrome numbers in that range, and I can think of a laughably simple way to simply generate those 900 numbers, right off the bat, in a fairly trivial manner without wasting a single CPU cycle on the remaining 899100 numbers. The shown code looks to be the worst possible solution to this simple puzzle. P.S. The shown code also tests 1000000, so it's really testing 900001 potential numbers, rather than 900000. You should fix that, at least. \$\endgroup\$ – Sam Varshavchik Oct 13 '17 at 21:23
  • 1
    \$\begingroup\$ Palindromes have very particular properties, so you should be able to generate these sequentially using patterns like A, AA, ABA, ABCBA and so on, iterating over all possible values for A, B, C and so on. This brute-force approach seems ridiculously inefficient. \$\endgroup\$ – tadman Oct 13 '17 at 21:32
6
\$\begingroup\$

I have checked your "brute force" algorithm and though it solves the problem (I have not run it to check), it will have to deal with a lot of work. Why not narrowing the problem a bit? What do we know about palindroms? They read the same from left to right than from right to left.

So if we use that and the fact that our palindroms are six digits long, we would only have to generate all the combinations for the first three digits, then make sure the next three are the same reversed. So:

for( int iter=100; iter<=999; iter++ ) {
  stringstream ss;
  ss << iter;
  string pal1 = ss.str();
  string pal2 = pal1;
  reverse(pal2.begin(), pal2.end());

  cout << pal1 << pal2 << endl;
}
\$\endgroup\$
0
\$\begingroup\$

As per my comment and the comment of @Sam Varshavchick, you program should like this :

#include <iostream>
#include <algorithm>
#include <sstream>

using namespace std;

int reverse(int a) {
    int r=0;
    while(a) {
        r=10*r+a%10;
        a=a/10;
    }

    return r;
}

int main(int argc, char* argv[] )
{

  for (int iter = 100; iter < 1000; ++iter ) {
        if(iter%10!=0)
            cout <<iter<<reverse(iter)<<endl;
        else
            cout <<iter<<"0"<<reverse(iter)<<endl;
  }
return 0;
}

You know what you are looking for, so you shouldn't test all the cases. You juste need to generate the numbers you want and no need to use string if you are simply going to output the result.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.