[JavaScript] Factorial Demo : 2004-01-14

http://www.faireal.net/demo/factorial

Required: Netscape 4.06+, Mozilla, or IE4+

Input a small number ( 0, 1, 2, ..., 10000 )
Factorial()
OR
Use Presets (NOTE: 10000! may be slow):

Output
Debug

How This Works

The basic idea is simple:

    var UNIT = Math.pow( 10, 12 );
    var myTable = [ 1 ];
    var nWork, i;
    for( var n = 2; n <= nSmallNumber; n++ ) {
        var nTableSize = myTable.length;
        var nOverflow = 0;
        for( i = 0; i < nTableSize; i++ ) {
            nWork = nOverflow + myTable[ i ] * n;
            myTable[ i ] = nWork % UNIT;
            nOverflow =  ( nWork - myTable[ i ] ) / UNIT;
        }
        if( nOverflow ) myTable[ i ] = nOverflow;
    }

When the loop is done, you have the result in myTable. Don't forget to do zero-paddings. For instance, if myTable[3] = 1234567890, it means "001234567890".

seelie317 (at) faireal.net

[index]