Tim Varley Logo
Tim Varley Principal AI Engineer and Tech Leader
Problem #4 easy

Largest Palindrome Product

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.

View on Project Euler

Implementations

cpp
// https://projecteuler.net/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 x 99.
// Find the largest palindrome made from the product of two 3-digit numbers.
// Answer: 906609
#include <iostream>
#include <cstdint>
using namespace std;
bool palindrome_test(uint64_t test_me)
{
uint64_t reversed = 0;
uint64_t original = test_me;
//cout << test_me << endl;
while( 0 < original ){
reversed = reversed * 10 + (original % 10);
original /= 10;
}
return (test_me == reversed);
}
uint64_t prob004_brute_force()
{
uint32_t max_pali = 0;
for( uint32_t i = 999 ; --i > 100 ;){
for( uint32_t j = 999 ; --j > 100;){
uint64_t t = i*j;
if(palindrome_test(t)){
if( t > max_pali ){
max_pali = t;
}
}
}
}
return max_pali;
}
#if ! defined UNITTEST_MODE
int main(int argc, char const *argv[])
{
cout << prob004_brute_force() << endl;
}
#endif // UNITTEST_MODE
View on GitHub
O(n) time complexity

Solution Notes

Mathematical Background

A palindromic number reads the same forwards and backwards (e.g., 121, 3443, 9009). For products of two n-digit numbers, the result will have at most 2n digits. The largest possible product of two 3-digit numbers is 999 × 999 = 998,001.

Palindromes have specific structural properties. A 6-digit palindrome can be represented as ABC CBA where A, B, C are digits, giving the number 100001A + 10010B + 1100C.

Algorithm Analysis

The implementations use a brute force approach: test all products of two 3-digit numbers and check if they’re palindromes. Key optimizations include:

  • Palindrome testing: Convert to string and compare with reverse (simple but effective)
  • Search space reduction: Start from largest numbers and work downwards to find largest palindrome first
  • Symmetry optimization: Some implementations avoid duplicate calculations (ij vs ji)
  • Early termination: Stop inner loop when product can’t exceed current maximum

Time complexity is O(n² × d) where n = 900 (3-digit numbers) and d is palindrome test cost.

Key Insights

  • The largest palindrome will be found among products of larger numbers
  • Starting the search from 999 downwards finds the answer quickly
  • String-based palindrome checking is simple and works for any digit length
  • The result 906609 = 993 × 913 demonstrates the search finds non-obvious factor pairs
  • This problem shows how brute force can be practical with reasonable bounds

Educational Value

This problem teaches important programming concepts:

  • Nested loop structures and iteration strategies
  • String manipulation and reversal algorithms
  • The trade-offs between different palindrome testing approaches
  • Optimization techniques for brute force algorithms
  • Understanding problem constraints to choose appropriate solution methods
  • The relationship between mathematical properties and computational efficiency