Project Euler – Problem 4

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.

#include <cstdio>
int reverse(int x){
	int y = x, z = 0;
	while(y){
		z = z * 10 + (y % 10);
		y /= 10;
	}
	return z;
}
int main(int argc, char **argv){

	int ans = 0;
	for(int a = 999; a > 1; --a)
		for(int b = a; b > 1; --b){
			int x = a * b, z = reverse(x);
			if(x == z && x > ans) ans = x;
		}
		printf("Answer: %d\n", ans);
		return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s