views:

185

answers:

2

My answer to this problem feels too much like these solutions in C.

Does anyone have any advice to make this more 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)))
+5  A: 

The clojure-euluer-project has several programs for you to reference.

Yin Zhu
Great resource...thanks.
dbyrne
+1 for the link!
Bart J
+2  A: 

I personally used this algorithm(which I found described here):

(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))))))

Seems to me much less complicated :-)

Bozhidar Batsov