Project Euler # 9 (Pythagorean triplets) in Clojure

My answer to this problem seems too similar to these solutions in C.

Does anyone have any tips to make this more lazy?

(use 'clojure.test) (:import 'java.lang.Math) (with-test (defn find-triplet-product ([target] (find-triplet-product 1 1 target)) ([ab target] (let [c (Math/sqrt (+ (* aa) (* bb)))] (let [sum (+ abc)] (cond (> a target) "ERROR" (= sum target) (reduce * (list ab (int c))) (> sum target) (recur (inc a) 1 target) (< sum target) (recur a (inc b) target)))))) (is (= (find-triplet-product 1000) 31875000))) 
+6
clojure
source share
2 answers

clojure -euluer-project contains several linking programs.

+7
source share

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

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

It seems to me much less complicated :-)

+4
source share

All Articles