---
title: "用clojure解决 euler problem 4"
author: "hotcharm"
date: "2012-07-07T21:12:00Z"
tags: []
slug: "clojure-euler-problem-4"
draft: false
lastmod: "2012-07-15T18:28:56Z"
id: 1594607
url: "https://hotcharm.iteye.com/blog/1594607"
---

**问题描述：**

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is

9009 = 91![×](http://projecteuler.net/images/symbol_times.gif)99.

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

**解决方案：**

```
(ns euler-problem-4.core
  (:use clojure.contrib.math))

(defn palindromic-number?
  [number]
  (loop
      [number-string (str number)]
    (if (or (empty? number-string) (= (count number-string) 1))
      true
      (if(= (first number-string) (last number-string))
        (recur (drop-last (rest number-string)))
        false))))

(defn largest-palindromic-3-digit
  []
  (loop
      [a 100, b 100, lpn 1]
    (if (and (= 999 a) (= 999 b))
     (if (palindromic-number? (* a b))
          (max (* a b) lpn)
           lpn)    
      (if (= a 999)
        (if (palindromic-number? (* a b))
          (recur 100 (inc b) (max (* a b) lpn))
          (recur 100 (inc b) lpn))
        (if (palindromic-number? (* a b))
          (recur (inc a) b (max (* a b) lpn))
          (recur (inc a) b lpn))))))

(largest-palindromic-3-digit)
```

答案：906609