Actually, the script I gave computes numbers in i.1e7 instead of 1e6. This
means that this method is a LOT faster than the previous one:

 

   6!:2 '+/"1 {&f 10&#.^:_1 i.1e7'

4.30622

 

If you wanted the number of times you do the operation and the length of the
list to actually match, you could start with 0 instead of f.

 

Marshall

 

 

From: Marshall Lochbaum [mailto:[email protected]] 
Sent: Sunday, September 05, 2010 5:58 PM
To: Programming forum
Subject: 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.

 

Marshall

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to