I presume the sides have to have integer lengths. Given that z<57%2, the maximum length is 28 and
28^3 21952 So the number of possibilities isn't too large, and we can use a brute force method. t=: s#:i.*/s=: 28 28 28 $t 21952 3 $ t=: (*./"1 (}:"1 t)<}."1 t)#t 3276 3 $ t=: (57=+/"1 t)#t 48 3 t 4 26 27 5 25 27 6 24 27 6 25 26 ... On Fri, May 24, 2013 at 9:28 AM, Robert Herman <[email protected]> wrote: > I was helping my son with his maths homework, and I thought it would be a > great chance to utilize J. Here is the problem: Given three sides, x, y and > z, where x<y<z and a perimeter of 57, with z < 1/2 of the perimeter, how > many triangles can you make that fit that criteria. Somebody created a perl > script with nested loops: > > for $x (1..57/2) { > for $y ($x+1..57/2) { > for $z ($y+1..57/2) { > if ($x + $y + $z eq 57) { > ++$count; > print "$count: $x $y $z\n"; > }}}} > print "Total: $count"; > > > I thought I could start to do it this way in J: > > x =. i..26 > y =. >:x > z =. >:y > > NB. Here's where I get stuck in trying to do a nested loop in J > +/" x y z NB. just to see if I could get it to sum each column > > I am stuck in the world of imperative programming and loops. I'm not > looking for the solution, but just a hint in which direction to take it. > I'm still fascinated with J, and hope to grasp array programming. Thank > you. > > Rob > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
