Lazy solution
This commit is contained in:
parent
d26d9d8ccc
commit
c611716050
|
@ -0,0 +1,19 @@
|
|||
(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))))))
|
Loading…
Reference in New Issue