# Project Euler Q4 :: Largest palindrome product

Explanation. Standard caveat: don’t look here if you are trying to do these yourself.

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.

This seems like another brute-force question. There’s not that many numbers to test.

## check the worked solution
91*99 # 9009


I’m not aware of an is.palindrome function, but it’s easy enough to code.

is.palindrome <- function(x) {
## convert to character and explode
x <- unlist(strsplit(as.character(x), ""))
## check if the vector is palindromic
return(identical(x, rev(x)))
}
is.palindrome(9009) # TRUE
is.palindrome(9001) # FALSE


Let’s try it out for the two digit example and make sure we’re on the right track. Multiply all two digit numbers together and test them for palindrome-ness, then find the largest of those.

twodigits <- 10:99
prods <- expand.grid(twodigits, twodigits)
prods$prod <- prods[ ,1]*prods[ ,2] prods.palindromes <- prods$prod[sapply(prods$prod, is.palindrome)] max(prods.palindromes) # 9009  Great! What about three digits? threedigits <- 100:999 prods <- expand.grid(threedigits, threedigits) prods$prod <- prods[ ,1]*prods[ ,2]
prods.palindromes <- prods$prod[sapply(prods$prod, is.palindrome)]
largest <- max(prods.palindromes)
largest # 906609

### CORRECT



Takes a little longer, and generates a nice little 10MB, 810,000 element vector along the way.

format(object.size(prods), units="Mb") # 9.4 Mb


The two three digit numbers?

prods[prods\$prod==largest, ] # 913 993