After contemplating on the comments on my previous solution to Problem 9 of Project Euler, I took a few minutes and looked into the for comprehension and then rewrote the method for generating the pythagorean triples.

I also had a comment pointing out that I was missing some triples generated, and that I was likely lucky in getting a triple that matched the criteria. When I was originally looking at wikipedia at the posting on Pythagorean Triple, I saw the following text:

Despite generating all primitive triples, Euclid's formula does not produce all triples.

I figured I was good with just generating all primitive triples, but as it was pointed out that I was missing some, I added in the functionality to generate the multiples of the triples as well.

The function pythagorean-triples has now been rewritten as:

(defn pythagorean-triples [] (for [m (range 2 500) n (range 1 m) k (range 1 500)] (map (partial * k) (pythagorean-triple-for m n))))

Thanks for the feedback, and this is much nicer, even with the extra computation for multiplying the triples by a K.

–Proctor

Pingback: Project Euler in Clojure – Problem 9 « Proctor It

Arcane SentimentIt’s even simpler if you don’t bother to generate Pythagorean triples at all – just generate all the triples that add up to 1000, and check whether a^2+b^2=c^2.