Dear Miridian

(I see some others have now posted useful replies, but this
might still be relevant)

Maybe there's a better way to solve the problem? 

In many of these Euler puzzles it is apparently quite easy
to solve the sample problem (2 by 9 in this case) with a
brute force approach.  This often helps me get a grasp
of the principles involved,  but it usually turns out that
my original method suffers from memory explosion or number
overflow or the like. 

Your array is (2^18) * 9 * 12 ~ 3e7, which should be fine
on a modern machine,  but its type is extended integer, so
its actual memory requirement can be huge depending on the
size of its largest element (I think).  It may be that a
sparse matrix approach will do;   however, I don't think $.
will support extended integers,   so you might need to
develop your own methods, presumably with an index array
and a value array. 

Or you could try a 4-d sparse array, eg   

   Mem=:1 $. ((2^18),2 9 12) ;0  2 3;_1

NB. ... set up array sparse with _1 as fill element

   Mem=:4 5 (0 0 1 2;0 1 1 2)}t 

NB. ... put value (4,5) in element (0,1,2)

   Mem        NB. show non-sparse elements

0 1 2 | 4 5
  
expressing your large integers x as (a,b)
where x = a + (b * 2^k) , a and b < 2^k for some large enough
2^k,  but that might get pretty messy. 

FWIW,  my method for 161 doesn't use a 3-d array.  It does 
use extended integers but only for the accumulator as far
as I can see - it's a long while since I solved it.  I find
two solvers in my script;  each takes about 20 seconds,  but
one needs 35 Mb compared to the other's 1.7 Mb.   No doubt
other J-Eulers can knock these down by an order of magnitude
or two!  My answers are pretty pedestrian.

Good luck with this and all the other problems!

Mike

miridian wrote:
> Hallo,
>
> I implemented a solution in J for Euler problem 161.
>
> It is very slow.
> :-(
>
> I use a big array to memoize the result and the amend operation
> is very slow.
>
> Here a snippet from my code
>
> time =: 6!:2
> W =: 9
> H =: 12
> C =: 2 ^ 2 * W
> Mem =: ( C, W , H ) $ _1x
>
> The amend take more than 4 seconds.
>
>     time 'Mem =: 4 ( < 1, 2, 3 ) } Mem'
> 4.136996
>
> Is there a way to improve the amend time?
>
> MfG
>
> Luca Masini.
>
> ----------------------------------------------------------------------
> 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