Math Programming: Computing Factorials

ASCII Format | APL+Win 3.6 Format | CS270 APL Workspace in APL+Win 3.6

The two APL functions listed below compute factorial two different ways. First is an implementation that computes the exact integer value of the factorial. The result is a character vector (string) of the exact value. It works for large values probably up to the amount of memory available to the APL workspace. The second function computes the approximate factorial for large integers up to a million or more.

With the text listing, linked above, you should be able to paste the functions into just about any version of APL other than IBM APL2.

Exact Factorial

[Exact Factorial]
[Exact Factorial - ADD0]
[Exact Factorial - DIGIT1]

Here's an example, suppose a simple cell, which can reproduce, consists of 300 ordered parts. The number of random combinations of parts would be

    EFAC 300

30605751221644063603537046129726862938858880417357699941677674125947653317
67168674655152914224775733499391478887017263688642639077590031542268429279
06974559841225476930271954604008012215776252176854255965356903506788725264
32189626429936520457644883038890975394348962543605322598077652127082243763
94491201286786753683057122936819436499564604981664502277165001851765464693
40112226034729724066333258583506870150169794168850353752137554910289126407
15715483028228493795263658014523523315693648223343679925459409527682060806
22328123873838808170496000000000000000000000000000000000000000000000000000
00000000000000000000000

Large Factorial

This little one liner computes factorials well beyond the precision of an APL double.

[Large Factorial]
Example:

FAC 300

3.060575122E614