Largest palindrome that can be made from a product of 2 3-digit numbers (Project Euler 4) This is my solution to Project Euler #4, which asks for the largest palindrome that can be made from a product of 2 3-digit numbers. I have done a brute force method, but it takes around 10-20 seconds to run. It works by looping through 2 variables, finding its product, and converting it to a string to check for palindromity. What steps can I take to improve the computational speed of this program?

#include &lt;cmath&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;iostream&gt;
int main()
{
int ans=0;
for(int i=100;i&lt;1000;i++)
    for(int j=100;j&lt;1000;j++)
    {
        int num_to_test=i*j;
        int num_of_digits=floor(log10(num_to_test))+1;
        string num_stringstream;
        ostringstream convert;
        convert &lt;&lt; num_to_test;
        num_stringstream=convert.str();
        int counter=0;
        for(int i=0;i&lt;num_of_digits;i++)
        {
            if(num_stringstream[i]==num_stringstream[num_of_digits-i-1])
            {
                counter++;
                if(counter==num_of_digits)
                    num_to_test&gt;ans?ans=num_to_test:ans=ans;
            }
        }
    }
cout &lt;&lt; ans &lt;&lt; endl;
char quit;
cin &gt;&gt; quit;
}
Speeding things up
There are several things you could do to speed things up:

Instead of looping from 100..999, go from 999..100 instead.  That way, if you find a palindrome, you can break from the j loop because any lower values of j will not beat the one you just found.
Once you loop backwards, you can also check i*j versus the best answer you've found so far.  If i*j &lt;= best, you can break from the j loop.
You don't have to use log10 to find the number of digits.  Since you are already converting the number to a string, just use num_stringstream.length().
The j loop can start at i instead of 999, because any j higher than i will already have been tested.

Cleaning things up
It would make your program look a lot more readable if you moved all of the palindrome checking logic to its own function.
This line here is very hard to read:

num_to_test&gt;ans?ans=num_to_test:ans=ans;


Putting it all together
Here is what I think your code should look like.  I removed the cin &lt;&lt; quit part because I wanted to time the program without requiring user input.
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;iostream&gt;

bool is_palindrome(int num)
{
    ostringstream convert;
    convert &lt;&lt; num;
    string num_stringstream = convert.str();
    int num_of_digits = num_stringstream.length();
    for (int i=0; i &lt; num_of_digits; i++)
    {
        if (num_stringstream[i] != num_stringstream[num_of_digits-i-1])
            return false;
    }
    return true;
}

int main(void)
{
    int best = 0;
    for (int i = 999; i &gt;= 100; i--)
    {
        for (int j = i; j &gt;= 100; j--)
        {
            int num_to_test = i * j;

            if (num_to_test &lt;= best)
                break;

            if (is_palindrome(num_to_test)) {
                best = num_to_test;
                break;
            }
        }
    }
    cout &lt;&lt; best &lt;&lt; endl;
}

Using gcc -O3, this program ran in 0.03 seconds on my computer as opposed to 1.06 seconds, for around a 30x speedup.