Project Euler Problem 4 Solution

Question

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

Find the largest palindrome made from the product of two 3-digit numbers.

Clojure

#!/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)))
$ time clojure palindrome.clj
real   0m1.115s
user   0m1.884s
sys    0m0.080s

Go

package 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)
}
$ go build -o palindrome palindrome.go
$ time ./palindrome
real   0m0.003s
user   0m0.000s
sys    0m0.000s

Haskell

isPalindrome ::  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]
$ ghc -O2 -o palindrome palindrome.hs
$ time ./palindrome
real   0m0.049s
user   0m0.048s
sys    0m0.000s

JavaScript

const 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)
$ time node --use-strict --harmony-destructuring palindrome.js
real   0m0.115s
user   0m0.112s
sys    0m0.000s

Python

#!/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]))
$ time python3 palindrome.py
real   0m0.293s
user   0m0.280s
sys    0m0.004s

Ruby

#!/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
$ time ruby palindrome.rb
real   0m0.372s
user   0m0.360s
sys    0m0.008s

Questions? Comments? Send me an email: z@chdenton.com