Your solution reminds me of the relationship between the Mobius function and the Mertens function, where mertens0=: +/ @: (mobius"0) @: >: @: i. http://www.jsoftware.com/jwiki/Essays/Mertens%20Function Because Mertens n "knows" that it's computing Mobius from 1 to n, it can be much faster than computing Mobius on each value.
----- Original Message ----- From: Marshall Lochbaum <[email protected]> Date: Monday, September 6, 2010 13:33 Subject: Re: [Jprogramming] splitting stuff into digits the way where you don't split it into digits To: 'Programming forum' <[email protected]> > The applicability is a bit of a problem, but you can offset it > with clever > working in bases. > Let lsf n give list of sums of factorials up to 10^n: > nexp=.(*10...@-@#) d=."."0@": n NB. Expanded > form (i.e. 1234-->1000 200 > 30 4) > start=.+/\ {&f d NB. Sums for starting values > (ie. 1***,12**,123*,1234) > start +&(+/) lsf"0 nexp > > That should do it. > > Marshall > > > -----Original Message----- > From: [email protected] > [mailto:[email protected]] On Behalf Of R.E. Boss > Sent: Monday, September 06, 2010 3:25 PM > To: 'Programming forum' > Subject: Re: [Jprogramming] splitting stuff into digits the way > where you > don't split it into digits > > Nice solution. Not only faster, but also leaner. > > rank '+/"1 {&f 10&#.^:_1 i.1e6';'([:,f&(+/))^:5 f' > +----+-----+-----+----+ > |rank|tm*sz|time |size| > +----+-----+-----+----+ > | 1 |94.60|14.89|6.35| > +----+-----+-----+----+ > | 0 | 1.00| 1.00|1.00| > +----+-----+-----+----+ > > -:/".&.> '+/"1 {&f 10&#.^:_1 i.1e6';'([:,f&(+/))^:5 f' > 1 > > However, only applicable if the universe is i.10^n. > > > R.E. Boss > > > > -----Oorspronkelijk bericht----- > > Van: [email protected] [mailto:programming- > > [email protected]] Namens Marshall Lochbaum > > Verzonden: zondag 5 september 2010 23:58 > > Aan: Programming forum > > Onderwerp: [Jprogramming] splitting stuff into digits the way > where > > you don't split it into digits > > > > Thought I would let everyone know about the alternate approach > I just > > found to the sums of factorials of digits problem: > > > > > > > > f=.(+!)i.10 > > > > r=.([:,f&(+/))^:6 f > > > > r=.r - 6,(9*10^i.7)#(i._7) > > > > > > > > This generates the sum of the digit plus the factorial for > each of > > i.1e6 > > > > This way, you don't have to actually work starting with i.1e6. > By > > modifying the 6s and 7s, you can generate any list of the form > 10^n, > > although 1e7 is about the limit in terms of memory. > > > > > > > > It is also faster than the other method: > > > > > > > > 6!:2 '([:,f&(+/))^:6 f=.(+!)i.10' > > > > 0.205724 > > > > 6!:2 '+/"1 {&f 10&#.^:_1 i.1e6' > > > > 0.378998 > > > > > > > > Where each has an error based on the number of leading zeros > > (corrected for in the last line of the script above). > > > > > > > > You can probably also generate length 10^2^n by taking > ([:,+/~)^:n f, > > but that is a bit ridiculous. ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
