Problem 4 - Largest Palindrome Product

Author

Vincent Clemson

Published

January 17, 2026

Modified

January 24, 2026

Sigirya, Sri Lanka - Sigirya Fortress - Lion Rock - May 5th, 2025

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is \(90009 = 91 \times 99\).
Find the largest palindrome made from the product of two 3-digit numbers.

https://projecteuler.net/problem=4

Development

R

Answer 1

Code
palindrome_m2n3 <- function(m = 2, n = 3) {
  # check from largest to least of product of m (10^n)-1 digit numbers
  x <- seq(((10^n)-1)^m, 1)
  y <- as.character(x)
  for (i in 1:length(x)) {
    # find reverse digits & check if palindrome 
    # then check for compatible n-digit m-factor product
    yr <- lapply(strsplit(y[i], ""), function(i) paste(rev(i), collapse = ""))[[1]]
    if (identical(y[i], yr)) {
      # n-digit sequence
      f_i <- seq((10^n)-1, 10^(n-1))
      # booleans of the factor
      f_x0 <- x[i] %% f_i == 0
      f_x <- x[i] / f_i
      # compatibility check: of factor 'f2'
      if (any(f_x0)) {
        # compatibility check: of n-digit factor 'f2'
        bool_n <- f_x[f_x0] %in% f_i
        if (any(bool_n)) {
          i_f <- min(which(f_i %in% f_x[f_x0]))
          f1 <- f_i[i_f]
          f2 <- f_x[i_f]
          return(data.frame(p = x[i], f1, f2, iter = i))
        }
      }
    }
  }
}

tic()
palindrome_m2n3(m = 2, n = 3)
toc()
       p  f1  f2  iter
1 906609 993 913 91393
0.591 sec elapsed

Python

Answer 1

Back to top

Reuse

© 2023-2026 Vincent Clemson | This post is licensed under <a href='http://creativecommons.org/licenses/by-nc-sa/4.0/' target='_blank'>CC BY-NC-SA 4.0</a>

Citation

For attribution, please cite this work as:
Clemson, Vincent. 2026. “Problem 4 - Largest Palindrome Product.” January 17, 2026. https://prncevince.xyz/euler/problem/0004.html.