Problem
A palindromic number reads the same both ways. The largest palindrome made from the product of two -digit numbers is .
Find the largest palindrome made from the product of two -digit numbers.
Solution
#include <algorithm>
#include <iostream>
#include <string>
bool palindrome(const std::string &s) {
int n = s.length();
for (int i = 0; i < n / 2; ++i) {
if (s[i] != s[n - i - 1]) {
return false;
}
}
return true;
}
int main(void) {
int ans = 0;
for (int i = 100; i <= 999; ++i) {
for (int j = 100; j <= 999; ++j) {
int x = i * j;
if (palindrome(std::to_string(x))) {
ans = std::max(ans, x);
}
}
}
std::cout << ans << std::endl;
return 0;
}