This fits in nicely somewhere in the elementary school years! I hope that 93 by 93 is rhe largest rectangle that will have a number under 1 million rectangles.
I would introduce these functions in a math class. It would be after they reach multiplication, f=: 13 :'(>:i.x) */>:i.y' g=: 13 :'(i.x<.y)<:/i.x>.y' all=: 13 :'+/,((x<.y) g x>.y) * (x<. y) f x >.y' A testing phase: 2 f 3 1 2 3 2 4 6 3 f 2 1 2 2 4 3 6 3 f 3 1 2 3 2 4 6 3 6 9 2 g 3 1 1 1 0 1 1 3 g 2 1 1 1 0 1 1 3 g 3 1 1 1 0 1 1 0 0 1 2 ALL 3 16 3 ALL 2 16 3 ALL 3 25 93 ALL 93 9689050 93 ALL 94 10099924 94 ALL 94 10108760 When computer science appears in elementary school, these functions could be revealed. f ([: >: [: i. [) */ [: >: [: i. ] g ([: i. <.) <:/ [: i. >. all [: +/ [: , (<. g >.) * <. f >. f=: 13 :'(>:i.x) */>:i.y' g=: 13 :'(i.x<.y)<:/i.x>.y' all=: 13 :'+/,((x<.y) g x>.y) * (x<. y) f x >.y' I don't think in the language of your question, so I can't help you much there. Linda -----Original Message----- From: programming-boun...@forums.jsoftware.com [mailto:programming-boun...@forums.jsoftware.com] On Behalf Of Tikkanz Sent: Tuesday, October 07, 2014 8:20 PM To: Programming JForum Subject: Re: [Jprogramming] Project Euler 85, Python and J Here is another version of countRects countRects=: */@(2 ! >:) On Wed, Oct 8, 2014 at 9:07 AM, Tikkanz <tikk...@gmail.com> wrote: > Sorry, yes that is a leap. > (x * (x + 1)) * 0.5 is the number of ways to choose two horizontal lines > to make 2 sides of the rectangle. > (y * (y + 1)) * 0.5 is the number of ways to choose two vertical lines to > make the other 2 sides of the rectangle > ((x * (x + 1)) * 0.5) * ((y * (y + 1)) * 0.5) is the number of ways to > choose the lines to make a rectangle > refactoring: > 4 %~ x * (x+1) * y * (y+1) > 4 %~ */ x,(x+1),y,(y+1) > 4 %~ */ x,y,(x+1),(y+1) > 4 %~ */ (, >:) x,y > > HTH > > On Wed, Oct 8, 2014 at 4:30 AM, Devon McCormick <devon...@gmail.com> > wrote: > >> Hi - >> >> "countRects" seems like a bit of a leap. I think I understand "4 %~" >> because you're overcounting by 4 rotations, but I don't comprehend the >> magic behind "*/@(,>:)". >> >> I see that "(,>:)" concatenates the shape to its increment, e.g. 2 3 3 4 >> for the input 2 3, but what's the rationale behind this? >> >> Thanks, >> >> Devon >> >> On Tue, Oct 7, 2014 at 7:41 AM, Tikkanz <tikk...@gmail.com> wrote: >> >> > Note that 200 x 200 is a bit of an overkill given 3x2 = 2x3 >> > The following choses the lower triangular of a matrix of the different >> > sized rectangles to investigate. >> > getSizes=: ,@(>:/~) # [: ,/ ,"0/~ >> > getSizes >: i. 5 >> > >> > Given the sides of a rectangle you can count the number of rectangles as >> > follows: >> > countRects=: 4 %~ */@(, >:) >> > countRects 2 3 >> > >> > Now get the index of the rectangle size with a count closest to 2million >> > >> > idxClosest=: (i. <./)@(2e6 |@:- ]) >> > >> > >> > Putting it together >> > >> > */@({~ idxClosest@:(countRects"1)) getSizes >: i.200 >> > >> > >> > >> > On Tue, Oct 7, 2014 at 5:37 PM, Jon Hough <jgho...@outlook.com> wrote: >> > >> > > Project Euler 85: https://projecteuler.net/problem=85 >> > > This problem is not really conceptually hard, but I am struggling >> with a >> > J >> > > solution.I have solved it in Python: >> > > ============================================= >> > > def pe85(larg, rarg): count = 0 llist = range(1, larg+1) >> > > rlist = range(1, rarg+1) >> > > for l in llist: for r in rlist: count >> += >> > > l*r >> > > return count >> > > >> > > if __name__ == "__main__": # test for 2x3 grid, as in question. >> k >> > > = pe85(2,3) print "Test value: "+str(k) l1 = >> range(1,200) # >> > > 200 lucky guess l2 = range(1,200) bestfit = 10000 # just a >> big >> > > number area = 0 for i in l1: for j in l2: >> > > diff = abs(2000000 - pe85(i,j)) if >> diff >> > < >> > > bestfit: area = i*j >> > > bestfit = diff >> > > print "AREA is "+str(area) >> > > >> > > >> > > ================================================The above script will >> > give >> > > the final area of the closest fit to 2 million. (The python code may >> not >> > be >> > > the best). Also I tested all possibilities up to 200x200, which was >> > chosen >> > > arbitrarily(~ish). >> > > Next my J. I go the inner calculation ok (i.e. see the function pe85 >> > > above). In J I have: >> > > pe85 =: +/@:+/@:((>:@:i.@:[) *"(0 _) (>:@:i.@:])) >> > > NB. I know, too brackety. Any tips for improvement appreciated. >> > > >> > > >> > > But from here things get tricky. If I do the calculation over 200x200 >> > > possibilities I end up with a big matrix, of which I have to find the >> > > closest value to 2 million, of which then I have to somehow get the >> (x,y) >> > > values of and then find the area by x*y. >> > > >> > > The main issue is getting the (x,y) from the best fit value of the >> array. >> > > >> > > i.e. If I do pe85"(0)/~ 200, I get a big array, and I know I can get >> the >> > > closest absolute value to 2 million but then I need to get the >> original >> > > values to multiply together to give the best fit area. Actually I have >> > > bumped into this issue many times. It is easy enough in a 1-d >> array,just >> > do: >> > > (I. somefunc ) { ]) >> > > >> > > or similar to get the index. But for two indices the problem is >> beyond me >> > > at the moment. Any help appreciated.Regards,Jon >> > > >> > > >> > > >> > > ---------------------------------------------------------------------- >> > > For information about J forums see >> http://www.jsoftware.com/forums.htm >> > > >> > ---------------------------------------------------------------------- >> > For information about J forums see http://www.jsoftware.com/forums.htm >> > >> >> >> >> -- >> Devon McCormick, CFA >> ---------------------------------------------------------------------- >> For information about J forums see http://www.jsoftware.com/forums.htm >> > > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm