Saturday, October 11, 2014

Project Euler Problem 9 Common Lisp

Project Euler Problem 9

This solution uses Dickson's method for generating Pythagorean triples and involved only two loops whereas other solutions seen used 3 loops with the Pythagorean equation to find a, b, and c.  Note the two loops make sure that m is always greater than n


(defun problem9 ()
  (loop for m from 1 below 1000
        do (loop for n from 1 below m
                 for m2 = (* m m)
                 for n2 = (* n n)
                 for a = (- m2 n2)
                 for b = (* 2 m n)
                 for c = (+ m2 n2)
                 if (= 1000 (+ a b c))

                 do (return-from problem9 (* a b c)))))

No comments:

Post a Comment