项目欧拉#9(毕达哥拉斯三胞胎)在Clojure

dby*_*rne 6 clojure pythagorean

我对这个问题的回答与C中的这些解决方案非常相似.

有没有人有任何建议让这更多lispy?

(use 'clojure.test)
(:import 'java.lang.Math)

(with-test
  (defn find-triplet-product
    ([target] (find-triplet-product 1 1 target))
    ([a b target]
      (let [c (Math/sqrt (+ (* a a) (* b b)))]
        (let [sum (+ a b c)]
          (cond 
            (> a target) "ERROR"
            (= sum target) (reduce * (list a b (int c)))
            (> sum target) (recur (inc a) 1 target)
            (< sum target) (recur a (inc b) target))))))

  (is (= (find-triplet-product 1000) 31875000)))
Run Code Online (Sandbox Code Playgroud)

Yin*_*Zhu 7

Clojure的-euluer项目有供你参考几个方案.