← Back to index
HardPHP3 min read

Largest Palindrome Product

find the largest palindrome made from the product of two n-digit numbers using brute force with optimizations.

mathbrute-force

Problem link

View on LeetCode

Solutions in this repo

  • PHP../largest-palindrome-product/solution.phpPHP solution

generate palindromes in descending order, then check if they can be factored into two n-digit numbers. start from the largest possible palindrome and work downwards.

for each palindrome, try to find factors by checking divisors from the largest n-digit number down. early termination when factors become too small.

optimization

generate palindromes efficiently by mirroring the first half. check divisibility starting from largest possible factor to minimize iterations.

complexity

O(10^(2n)) worst case, but optimizations make it much faster in practice. O(1) space.