Project Euler #9 (Pythagorean triplets) in Clojure

Posted by dbyrne on Stack Overflow See other posts from Stack Overflow or by dbyrne
Published on 2010-06-03T15:20:21Z Indexed on 2010/06/03 15:24 UTC
Read the original article Hit count: 256

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about clojure

Related posts about project-euler