I don't know about the rank thing, but you get a pretty huge speedup (ie.
more than a factor of 10) if you use the special code for digits and make
(+!) into a lookup table:

f=.(+!) i. 10
special2=.  = +/@({&f)@("."0@":"0)

since the whole thing is rank 0 due to the "0 on the digits, it gives the
correct answers even with leading zeros.

The only way I see to get around the system padding 0s onto the beginning is
to use boxes, which is WAY more trouble than it's worth.

Marshall


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Alex Gian
Sent: Sunday, August 29, 2010 12:05 PM
To: Programming forum
Subject: Re: [Jprogramming] Splitting an integer into its digits

Hi Ric and all,

There's one more thing I'd like to mention, now that I got this 'rank'
thing sorted:

I don't know if you noticed, but adding "0 to the 'digits' verb, has some
interesting, and IMHO not very acceptable effects on time and space taken.

For instance, finding specials <10e5 is instantaneous on my desktop machine
without the "0, yet takes about 7 seconds when done the other way.  in the
process it uses all of my 2Gb memory!
(OK, without the "0 means using the ugly transpose method AND getting the
wrong results for leading zeros, but still.  I guess J really does prefer
big arrays!!) Trying to find all specials <10e6, eats up all mem PLUS my 6Gb
swap space without achieving a result.  Without the "0 I get the result 1n 2
seconds, albeit with the wrong value for numbers with leading-zeros.

On my PDA (Acer n30, which is my main J learning platform), it doesn't even
finish without the "0, complains it's out of memory.  
I dragged out my old Jornada 928 (J502) and it managed to finish here,
probably a bit more memory available.  Took 18 seconds on what is basically
a 206MHz ARM.  
In comparison, my Nokia 770, with a 260MHZ ARM, solves the same problem
iteratively, in Perl, in 28 seconds, without taking any space at all.

I don't think this reflects too well on J.  I suppose what I'm asking
is:  is there another (neat J-like) way to do searches, other than using #
over huge arrays, that would take up little (preferably constant) space for
this class of problems?




On Sun, 2010-08-29 at 02:35 +1200, Sherlock, Ric wrote:

> If you add the "0 to digits, you don't need to use the transpose because
+/ is only ever getting fed a list (rank 1). 
> 
>    digits=: 10&#.^:_1"0
>    fp1=: + !
>    spsum=: +/@f...@digits
>    special=: = spsum
>    (#~ special)  1974 123 85837 408297
> 85837 408297
> 
>    special f.
> = +/@(+ !)@(10&#.^:_1"0)
> 
> IOW a one-liner for special would be:
>    special2=: = +/@(+ !)@(10&#.^:_1"0)
>    (#~ special2) 1974 123 85837 408297
> 85837 408297
> 
> ----------------------------------------------------------------------
> 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