Thanks, Bill.  I had tried a bunch of different bracketings, but not this
one.

APL, is a way was simpler, nothing worked from left to right, everything
worked from right to left.  I understand why conjunctions have to bind left
to right, I think, but what I don't understand is why

 (5&{.!.1) /. (5 5 $i. 25)

fails while yours yours works.

The entire program (with your mod to bracketing is:

 >./(,4 */\ a) ,(,4 */\ |:a) ,(,4*/\"1 (20&({.!.1)/.a)),(,4*/\"1
(20&({.!.1)/.|:a))

The point is to take the largest of products of groups of four, overlapping,
on the verticals, horizontals, or diagonals. 'a' is pre-loaded with the
20x20 matrix.


>./(,4 */\ a) ,(,4 */\ |:a) ,(,4*/\"1>20{.!.1 L:_1 </.a),(,4*/\"1>20{.!.1
L:_1 </.|:a)

My solution was above, but I was extending every line separately, then
unboxing the array and and while it does not matter for this 20x20 array, I
want to learn techniques that I can apply to other problems, and to J in
general.  I thought that once I finished reading JforC, I would practice
programs, and then I discovered projecteuler.org.  So this is not just about
solving problems, although there have been a couple I just gave up on J for
solving because the search space was too big. One, for example, I allowed J
to work on for 12 hours and it was not memory short.  I wrote a D solution
that ran in a couple seconds.  That one

First off, the actual solution can be found in the verticals and horizontals
- I had tried a partial solution, put a row of 1's onto the array so that it
was 20x21 and reshaped it so that it was 21x20, but that didn't really work.
 I had read how /. worked, but I thought it only took maximum length
diagonals.  Wrong presumption.  >./(,4 */\ a) ,(,4 */\ |:a) gets you into
the forums, but I thought that was a cheat, since I had ignored the hard
part of the problem. I got the boxed version working, and then I wanted to
code it without boxes.

I first went looking to see if I could specify a fill to unbox, but there is
no such operation.  Reading the discussion here, I see that it is likely
that the fill is applied separately, not by unbox.  It just presents the
line of the array, and the system makes the array regular, which only allows
for a zero fill. Because I want the last shard to go into the candidate
list, and I don't care that there are extra 1's there, I decided to try the
1 fill, and that could be done with boxes.

I saw the solution that involved boxing the diagonals and I guess I could
have just done groups of four on the boxed diagonals.  I have solved a
couple of the problems with severe performance problems and I got on the
forum and there was a much faster solution, and the main difference between
my solution is that I boxed permutations of the stuff while when I got into
the forums I found much faster solutions that used tables to create
permutations using u/~, and in a couple of cases they got most of their
performance boost by handling more data at once than I was.  So I'm leery of
boxing, and try not to use it, unless I am doing something like sorting line
by line.

So I wanted to do something without boxing.  When I looked at the solution,
I saw that the /. adverb didn't just restrict itself to full length
diagonals, and that led me to making a rectangular table with 1 fills.


On Sun, Sep 25, 2011 at 3:06 AM, bill lam <[email protected]> wrote:

> No idea on the problem or forgotten.
>
> conjunction binds to the left so that you need brackets, eg.
>    5&({.!.1) /. (5 5 $i. 25)
>
> or if you are only interested in the product, it might be
>
>       *//. (5 5 $i. 25)
>

I need the products of groups of four, and can't find any bracketing of (4
(*/)\)/. (5 5 $i. 25) that  does not produce an error.

no, the problem calls for products in groups of four

>
> Вск, 25 Сен 2011, Nick Simicich писал(а):
> > I'm working on euler 11 from projecteuler.org.  I got the answer by
> doing
> > something really crummy, and I decided to go back and do proper code for
> the
> > diagonals.  The problem is to take the greatest product of four
> consecutive
> > numbers in a 20 by 20 array where the possibilities are rows, columns and
> > diagonals.
> >
> > Rows and columns are trivial, and now that I understand that /. does not
> > only take the longest diagonal the diagonals are not much harder.
> >
> > Because this is a product, I wanted to use a 1 fill.
> >
> > a is a 20 by 20 array of small numbers.
> >
> > My first attempt was to box the diagonals, and then fill them, the unbox
> > them and then do my product on the crosswise bits, and then take the max,
> > so
> >
> > >./(,4 */\ a) ,(,4 */\ |:a) ,(,4*/\"1 > 20{.!.1 L:_1 </.a),(,4*/\"1 >
> > 20{.!.1 L:_1 </.|:a)
> >
> > I transpose the array to get the other diagonals, and do it again so that
> I
> > can do the horizontals and verticals.
> >
> > I used a box for the diagonals because my thought was that they were all
> > different lengths, so they needed to be boxed. Once I fixed them to be
> > conformant, I could unbox them.
> >
> > Then I looked and determined that I could get an array with the diagonals
> on
> > the lines as:
> >
> > 20&{. /.aa
> >
> > And this gives (the small, forum appropriate version):
> >   5&{. /. (5 5 $i. 25)
> >  0  0  0  0  0
> >  1  5  0  0  0
> >  2  6 10  0  0
> >  3  7 11 15  0
> >  4  8 12 16 20
> >  9 13 17 21  0
> > 14 18 22  0  0
> > 19 23  0  0  0
> > 24  0  0  0  0
> >
> > But because it is a product, I need a 1 fill.  I am using a 1 fill with
> take
> > above, when I am taking from the lines individually.
> >
> >   5&{.!.1 /. (5 5 $i. 25)
> > |domain error
> > |       5&{.!.1/.(5 5$i.25)
> >
> > I could not, at first, figure out why the domain error was occuring.
>  Take
> > should fix the length of the line no matter what the length of the
> incoming
> > line.
> >
> > Then I thought, suppose it is calculating a number of things to add to
> the
> > vector, and the second vector it tries to finesse the calculation, using
> the
> > same value to produce a line, and then having a domain error when that
> value
> > of pad produces an unsquare array.  What if I change the rank of the verb
> to
> > try and get it, say, not to run on all of the lines produced by /. at the
> > same time, but instead to run on each line?  So I tried a few different
> > values for the rank.
> >
> >   (5&{.!.1)"_1 /. (5 5 $i. 25)
> > |domain error
> >
> > I won't bore you by repeating this, but that theory simply didn't work. I
> > could not get the verb to stop reporting an error.
> >
> > 0+/. a builds me a zero filled array that looks exactly like 20{. .
>  Can't
> > use 1+ or a verb with a test assuming I could get it to work (usually
> can't,
> > I'm new to this) because there are zeros in the array that I think I have
> to
> > leave alone.
> >
> > Maybe the real problem is that the fill is not coming from where I expect
> it
> > to come from. But I think they come from the take verb when I do 20{.
> > /. aand well, this is interesting:
> >
> >  (#,5&{.) /. (5 5 $i. 25)
> > 1  0  0  0  0  0
> > 2  1  5  0  0  0
> > 3  2  6 10  0  0
> > 4  3  7 11 15  0
> > 5  4  8 12 16 20
> > 4  9 13 17 21  0
> > 3 14 18 22  0  0
> > 2 19 23  0  0  0
> > 1 24  0  0  0  0
> >
> >
> > so the lines are clearly not being padded before the verb is called.
> >
> > and, um-- well, I think I just don't understand things more complex, it
> > implies that if I can derive the length I should be able to prepend
> > (5-#L$1), instead of hash but I always get hung in anything complex.
> >
> > Is this a bug or a feecher?  Other than boxing it, is there a way to code
> > it?  Am I just missing how to do it?
> >
> >
> >
> >
> >
> > --
> > Of course I can ride in the carpool lane, officer.  Jesus is my constant
> > companion.
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
>
> --
> regards,
> ====================================================
> GPG key 1024D/4434BAB3 2008-08-24
> gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm




-- 
Of course I can ride in the carpool lane, officer.  Jesus is my constant
companion.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to