問題
左右どちらから読んでも同じ値になる数を回文数という。 2桁の数の積で表される回文数のうち、最大のものは 9009 = 91 × 99 である。では、3桁の数の積で表される回文数のうち最大のものはいくらになるか。
答えを求めたコード
999から回文数を求めて、最も大きい数字を最後に取り出すだけの単純なコードです。private Boolean isPalindrome(int num) {
num.toString() == num.toString().reverse()
}
private Integer getMaxPalindrome(int num) {
(num .. 100).findResult {
if (isPalindrome(num * it)) {
num * it
}
}
}
def ret = (999..100).collect {
getMaxPalindrome(it)
}
println "{ret.max()} = ${ret.max()}"