-4

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Write a program that finds the largest palindrome made from the product of two 3-digit numbers.

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 3
    Welcome to stackoverflow! Your question is unclear, please read and edit your question according to [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so that other users can help you. Also, add expected output and code that you have tried. – pogibas Dec 02 '19 at 11:29
  • Related in Java: https://stackoverflow.com/q/7183977/680068 – zx8754 Dec 02 '19 at 11:47
  • Related post in R https://stackoverflow.com/questions/3763518/reverse-digits-in-r – zx8754 Dec 02 '19 at 11:56

1 Answers1

1

Try the code below:

ispalindromic <- function(x) {
  return(all(utf8ToInt(x) == rev(utf8ToInt(x))))
}

v <- 100:999
l <- sapply(as.character(outer(v,v)), ispalindromic)
r <- as.numeric(names(tail(which(l),1)))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • A while loop that checks from the largest number down should be substantially faster. `l <- as.character(outer(v,v)); k <- TRUE; i <- length(l); while (k) { if (ispalindromic(l[i])) { print(l[i]); k <- FALSE; } i <- i - 1 }`. – Axeman Dec 02 '19 at 18:10
  • @Axeman yes, that's true, searching from the tail would be much faster – ThomasIsCoding Dec 02 '19 at 18:50