It seems to do what you want for low arguments.
eg
    */\>:i.10
1 2 6 24 120 720 5040 40320 362880 3628800
    f"0 >:i. 10
1 2 6 24 12 72 504 4032 36288 36288

However,  I was wondering about avoiding the array,
so tried:
    g =: (100000&| @ rtz @ */), >:@{:

    |:g ^:(>:i.10) 1 1
1 2 6 24 12 72 504 4032 36288 36288
2 3 4  5  6  7   8    9    10    11

NB. the paired results are (factorial fragment for i), (i+1)

No problems so far,  but consider:
    |:g ^:(10+i.10) 1 1
36288 99168 90016 70208 82912 24368 89888 28096 5728 8832
    11    12    13    14    15    16    17    18   19   20

       f"0 ] 10 + i. 10
36288 99168 90016 70208 82912 74368 89888 28096 5728 8832

But
    82912 * 15
1243680

... so I think that 24368 is correct,  but can't quite see
why f is failing at 15, when g doesn't! The error is only
in the highest order digit, so the sequence recovers at 16.

It does of course turn out that g is slower than f, even
though it uses much less space.

If you need factorials of a very large number,  you might to
better to consider the number of occurrences of each
prime less than or equal to that number in all list elements,
taking particular care with the twos and fives in order to
"remove" those trailing zeros.
    eg consider:

    __ q:!10
2 3 5 7
8 4 2 1

    10 +/@<.@ % each 2 4 8 ; 3 9;  5; 7
+-----+---+-+-+
|5 2 1|3 1|2|1|
+-----+---+-+-+

    10 (+/@:(<.@ %)) each 2 4 8 ; 3 9;  5; 7
+-+-+-+-+
|8|4|2|1|
+-+-+-+-+


Mike

On 01/11/2011 5:23 PM, David Vaughan wrote:
> I am trying to get the factorial of very large numbers, but I only care about 
> the last 5 digits before the trailing zeros. So, I was hoping to do this:
>
> rtz =: %&10 ^: (0: = 10&|) ^:_                        NB. remove trailing 
> zeros of y
> f =: 100000&| @ rtz @ */@:>:@i.               NB. get the last 5 non-zero 
> digits of y
>
> Though I am not sure that it is doing exactly as I was hoping.
>
> My question is, does f do 100000&| @ rtz in between every multiplication or 
> does it actually only apply itself at the end once?
>
> Thanks.
> ----------------------------------------------------------------------
> 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