7c2948c7ec11

Problem 9
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sun, 10 Apr 2016 19:31:21 +0000
parents 20c03264a55e
children d4b651730553
branches/tags (none)
files euler.lisp

Changes

diff -r 20c03264a55e -r 7c2948c7ec11 euler.lisp
--- a/euler.lisp	Sun Apr 10 19:17:38 2016 +0000
+++ b/euler.lisp	Sun Apr 10 19:31:21 2016 +0000
@@ -134,6 +134,16 @@
    (loop :for window :in (n-grams 13 digits)
          :maximize (apply #'* window))))
 
+(defun problem-9 ()
+  (flet ((pythagorean-triplet-p (a b c)
+           (= (+ (square a) (square b))
+              (square c))))
+    (block search
+      (loop :for c :from 998 :downto 1 ; they must add up to 1000, so C can be at most 998
+            :do (loop :for a :from (- 999 c) :downto 1 ; A can be at most 999 - C (to leave 1 for B)
+                      :for b = (- 1000 c a)
+                      :when (pythagorean-triplet-p a b c)
+                      :do (return-from search (* a b c)))))))
 
 ;;;; Tests
 (def-suite :euler)
@@ -147,6 +157,7 @@
 (test p6 (is (= 25164150 (problem-6))))
 (test p7 (is (= 104743 (problem-7))))
 (test p8 (is (= 23514624000 (problem-8))))
+(test p9 (is (= 31875000 (problem-9))))
 
 
 ; (run! :euler)