project-euler/p85.lisp

20 lines
729 B
Common Lisp

(defun count-rectangles (max-width max-height)
(loop with count = 0
for width from 1 upto max-width
do (loop for height from 1 upto max-height
do (incf count (* (1+ (- max-width width))
(1+ (- max-height height)))))
finally (return count)))
(defun counting-rectangles (&optional target)
;; TODO: Could terminate once height 1 is over target, right?
(loop with best-difference = 10000000
for width upfrom 1
do (loop for height from 1 upto width
for count = (count-rectangles width height)
for difference = (abs (- target count))
do (when (< difference best-difference)
(setf best-difference difference)
(format t "(~a) ~ax~a (~a)~%" difference width height (* width height))))))