Yes, that also works.

-- 
Raul

On Fri, May 24, 2013 at 2:35 PM, Elton Wang <[email protected]> wrote:
> To modify my answer to count in x<y<z,
> We only need
> +/+/+/(57=x+/y+/z)*x</y*y</z
> Where x=.y=.z=i.29
>
> Answer is 61
>
> On May 24, 2013, at 2:13 PM, Raul Miller <[email protected]> wrote:
>
>> On Fri, May 24, 2013 at 12:28 PM, 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
>>
>> You are good up to this point.
>>
>> As Elton Wang points out, you can use x+/y+/z to generate all possible
>> permutations for x y and z.  But note that this does not distinguish
>> between different versions of the same triangle.  In other words,
>> these are probably all the same triangle:
>>   18+(i.!3) A. i.3
>> 18 19 20
>> 18 20 19
>> 19 18 20
>> 19 20 18
>> 20 18 19
>> 20 19 18
>>
>> The perl program avoids this issue by enforcing that $x < $y and $y < $z.
>>
>> In J you could generate all the possibilities, sort them (achieving
>> the perl constraint) and then discard duplicates.  Here's some hints
>> about that approach:
>>
>>   x=:1+i.26
>>   y=:1+x
>>   z=:y
>>   $,{x;y;z
>> 17576
>>   $~./:~&.>,{x;y;z
>> 3626
>>   $>{x;y;z
>> 26 26 26 3
>>   $>,{x;y;z
>> 17576 3
>>   $>~./:~&.>,{x;y;z
>> 3626 3
>>
>>> 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.
>>
>> There are other ways of approaching this problem, also. Conceptually
>> speaking, you do not really have three independent values.  Once you
>> have two values, you know the third (57-x+y), and you might need to
>> discard some cases (where z is negative, and where y is not greater
>> than x).
>>
>> I hope this helps.
>>
>> --
>> Raul
>> ----------------------------------------------------------------------
>> 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

Reply via email to