Skip to content

Instantly share code, notes, and snippets.

@hroi
Created February 24, 2010 00:00
Show Gist options
  • Save hroi/312884 to your computer and use it in GitHub Desktop.
Save hroi/312884 to your computer and use it in GitHub Desktop.
; Problem 9
; A Pythagorean triplet is a set of three natural numbers, a < b < c,
; for which,
;
; a^2 + b^2 = c^2
; For example, 3^2 + 4^2 = 9 + 16 = 25 = 52.
;
; There exists exactly one Pythagorean triplet for which a + b + c = 1000.
; Find the product abc.
(defn pythagorean? [a b c]
(= (+ (* a a) (* b b)) (* c c)))
(defn pythagorean-triplets-with-sum [n]
(for [a (range 1 (- n 3))
b (range (inc a) (- n 3))
:let [c (- n a b)]
:when (pythagorean? a b c)]
[a b c]))
(def answer
(reduce * (first (pythagorean-triplets-with-sum 1000))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment