I expect the following to be faster:

Apply "digits" to the whole array at once, and also
apply (+!) to the result of that.  But before you apply 
+/"1, do something about the elements corresponding 
to leading 0 digits, e.g.  (* -.@:(*/\"1)@:(0&=)) .



----- Original Message -----
From: "Sherlock, Ric" <[email protected]>
Date: Saturday, August 28, 2010 7:37
Subject: Re: [Jprogramming] Splitting an integer into its digits
To: Programming forum <[email protected]>

> > From: Alex Gian
> > Sent: Sunday, 29 August 2010 02:01
> > 
> > Thanks a lot for that Ric.
> > 
> > I actually managed to get as far as this:
> >  digits =. 10&#.inv
> >  fp1 =. +!
> >  spsum =. +/ @ |: @ fp1 @ digits
> >  special =. = spsum
> >  (#~ special)i.100000
> > 
> > which gave me 85837 correctly
> > also,
> >  (#~ special)i.1000000
> > 41086 408297
> > gives the correct larger number in very acceptable time, the lower
> > number is incorrect because of the leading-zero prob already 
> mentioned.> 
> > I really hate what I had to do with the transpose to get the sum,
> > though, so I'll be studying your example rigorously.  
> Seems to
> > eliminate all these probs.
> 
> 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

Reply via email to