Problem
A palindromic number reads the same both ways. The largest palindrome
made from the product of two 2-digit numbers is $9009 = 91 \times 99$.
Find the largest palindrome made from the product of two 3-digit numbers.
Answer
1 | 906609 |
Python1
2#!/usr/bin/env python
print(max(a * b for a in range(100, 1000) for b in range(a, 1000) if str(a * b) == str(a * b)[::-1]))
JavaScript1
2
3
4
5
6
7
8
9
10
11const isPalindrome = (s) => s === s.split("").reverse().join("")
let max = 0
for (let i = 100; i < 1000; i++) {
for (let j = 100; j < 1000; j++) {
const s = (i * j)
if (s > max && isPalindrome(s.toString())) {
max = s
}
}
}
console.log(max)
Ruby1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#!/usr/bin/env ruby
class Integer
def palindromic?
digits = self.to_s.split('')
return digits == digits.reverse
end
end
max = 0
(100..999).each do |a|
(a..999).each do |b|
product = a * b
if product > max and product.palindromic?
max = product
end
end
end
puts max
Go1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27package main
import "fmt"
func IsPalindrome(number int) bool {
n := number
reversed, digit := 0, 0
for n > 0 {
digit = n % 10
reversed = reversed*10 + digit
n /= 10
}
return number == reversed
}
func main() {
product, max := 0, 0
for a := 100; a < 1000; a++ {
for b := a; b < 1000; b++ {
product = a * b
if product > max && IsPalindrome(product) {
max = product
}
}
}
fmt.Println(max)
}
Haskell1
2
3
4
5isPalindrome :: Integer -> Bool
isPalindrome n = show n == reverse (show n)
main :: IO ()
main = print $ maximum [prod | a <- [100..999], b <- [a..999], let prod = a * b, isPalindrome prod]
Clojure1
2
3
4
5
6
7
8#!/usr/bin/env clojure
(defn palindrome? [n]
(= (seq (str n)) (reverse (str n))))
(defn palindromes [limit]
(filter palindrome? (for [a (range 100 1000) b (range a 1000)] (* a b))))
(println (reduce max (palindromes 1000)))
Rust1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23fn is_palindrome(number: u64) -> bool {
let mut n = number;
let mut reversed = 0;
while n > 0 {
let digit = n % 10;
reversed = reversed * 10 + digit;
n /= 10;
}
number == reversed
}
fn main() {
let mut largest = 0;
for a in 100..1000 {
for b in 100..1000 {
let product = a * b;
if product > largest && is_palindrome(product) {
largest = product;
}
}
}
println!("{}", largest);
}
Java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24public final class p004 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p004().run());
}
/*
* Computers are fast, so we can implement this solution directly without any clever math.
* Note that the maximum product is 999 * 999, which fits in a Java int type.
*/
public String run() {
int maxPalin = -1;
for (int i = 100; i < 1000; i++) {
for (int j = 100; j < 1000; j++) {
int prod = i * j;
if (Library.isPalindrome(prod) && prod > maxPalin)
maxPalin = prod;
}
}
return Integer.toString(maxPalin);
}
}
Mathematica1
2PalindromeQ[x_] := IntegerDigits[x] == Reverse[IntegerDigits[x]]
Max[Select[Flatten[Table[i * j, {i, 100, 999}, {j, 100, 999}]], PalindromeQ]]