用clojure解决 euler problem 4

问题描述:

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

9009 = 9199.

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


你可能感兴趣的:(clojure)