Clojure中的Euler#9项目(毕达哥拉斯三胞胎)

Clojure中的Euler#9项目(毕达哥拉斯三胞胎),clojure,pythagorean,Clojure,Pythagorean,我对此的回答感觉太像这些了 有没有人有什么建议可以让这变得更简单 (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 [su

我对此的回答感觉太像这些了

有没有人有什么建议可以让这变得更简单

(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)))
(使用“clojure.test”)
(:import'java.lang.Math)
(带测试)
(defn)查找三重态乘积
([目标](查找三联体产品1目标))
([a b目标]
(设[c(数学/sqrt(+(*a)(*b)))]
(让[总和(+a b c)]
(续)
(>目标)“错误”
(=总和目标)(减少*(列表a b(整数c)))
(>总和目标)(重复(包括a)1个目标)
(
有几个程序供您参考

我个人使用了这个算法(我发现它被描述了):

在我看来,要简单得多:-)

(defn generate-triple [n]
  (loop [m (inc n)]
    (let [a (- (* m m) (* n n))
          b (* 2 (* m n)) c (+ (* m m) (* n n)) sum (+ a b c)]
      (if (>= sum 1000)
        [a b c sum]
        (recur (inc m))))))