! Copyright (c) 2009 Guillaume Nargeot. ! See http://factorcode.org/license.txt for BSD license. USING: accessors kernel math math.ranges project-euler.common sequences locals ; IN: project-euler.085 ! http://projecteuler.net/index.php?section=problems&id=85 ! DESCRIPTION ! ----------- ! By counting carefully it can be seen that a rectangular grid measuring ! 3 by 2 contains eighteen rectangles. ! Although there exists no rectangular grid that contains exactly two million ! rectangles, find the area of the grid with the nearest solution. ! SOLUTION ! -------- ! A grid measuring x by y contains x * (x + 1) * y * (x + 1) rectangles. result : min-by-distance ( seq seq -- seq ) [ [ distance>> ] bi@ < ] most ; inline : compute-result ( i j -- pair ) [ * ] [ rectangles-count distance ] 2bi ; inline : area-of-nearest ( -- n ) T{ result f 0 2000000 } 1 2000 [ compute-result min-by-distance ] each-unique-product area>> ; PRIVATE> : euler085 ( -- answer ) area-of-nearest ; ! [ euler085 ] 100 ave-time ! 2285 ms ave run time - 4.8 SD (100 trials) SOLUTION: euler085