Project Euler4
Problem4
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.
回文数(左から読んでも右から読んでも同じ数)かつ、2つの3桁の数の積で表されるもののうち最大のものを求めよ、という問題。
1000000未満の範囲で回文数をまず求めて、その後100から999で片っ端から除して答えを求めた。
pal <- NULL for(a in 0:9){ for(b in 0:9){ for(c in 0:9){ pal0 <- a*100000 + b*10000 + c*1000 + c*100 + b*10 + a if(pal0 >= 10000){ pal <- c(pal, pal0) }else{ next} } } } for(num in 100:999){ res0 <- pal[pal%%num==0 & pal%/%num>=100 & pal%/%num<=999 & pal/num>=100 & pal/num<=999] if(sum(res0)==0){ next }else{ res <- unique(c(res, res0)) } } ans <- max(res)