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.
palindrome_m2n3<-function(m=2, n=3){# check from largest to least of product of m (10^n)-1 digit numbersx<-seq(((10^n)-1)^m, 1)y<-as.character(x)for(iin1:length(x)){# find reverse digits & check if palindrome # then check for compatible n-digit m-factor productyr<-lapply(strsplit(y[i], ""), function(i)paste(rev(i), collapse =""))[[1]]if(identical(y[i], yr)){# n-digit sequencef_i<-seq((10^n)-1, 10^(n-1))# booleans of the factorf_x0<-x[i]%%f_i==0f_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_iif(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