Project Euler in Clojure – Problem 9 Redux

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.



, ,

  1. #1 by Arcane Sentiment on July 16, 2012 - 13:45

    It’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.

  1. Project Euler in Clojure – Problem 9 « Proctor It

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: