Project Euler #4
Posted on May 26, 2010
This one also took a bit more time than I would have liked, but after fixing the problem I understood how I missed it. Before if the left value ( left * right ) was higher it'd be considered the "largest" palindrome, which was incorrect. I hadn't realized this until I started "debugging" my thoughts and the math behind everything, eventually I checked made the program check the next palindrome up to see if it were larger than the current "answer", if it was larger it'd replace it as the answer. This is pretty easy to understand, so I won't go into explaining it:
int Reverse( int n )
{
int tO = n, t1 = 0;
while( tO > 0 )
{
t1 *= 10;
t1 += tO % 10;
tO /= 10;
}
return t1;
}
int Problem4()
{
int Answer = 0;
int Maybe = 0;
for( int i = 999; i >= 100; i-- )
{
for( int c = 999; c >= 100; c-- )
{
if( ( i * c ) == Reverse( i * c ) )
{
Maybe = ( i * c );
if( Maybe > Answer ) Answer = ( i * c );
}
}
}
}