Monday, December 16, 2013

Size of Precomputed Factorial Lookup Tables

Here is a precomputed factorial lookup table for 1-1000 (e.g. the first 1000 factorials). (Size: 1.12 MB)
The python source code is:
import math
for x in range(0,1001):
print str(x) + ' ' + str(math.factorial(x))

The size of the factorial lookup table for 1-8761 is 120 MB.

Here is an approximation to calculate the size of a factorial lookup table which lists the factorials of the numbers 1 - 1000 using Stirling's approximation:

http://www.wolframalpha.com/input/?i=sum+log10%28%5Csqrt%7B2*%5CPi*n%7D%5Cfrac%7Bn%7D%7Be%7D%5En%29%2C+n%3D1+to+1000



Integral approximation for large N:
http://www.wolframalpha.com/input/?_=1387257739270&i=integrate+log10(%5csqrt%7b2*%5cPi*n%7d%5cfrac%7bn%7d%7be%7d%5en)+from+n%3d1+to+10%5e8&fp=1&incTime=true


Here is a table of the approximate sizes of factorial lookup tables for 1-N.
N=1 up to and including N=10^7 use the summation approximation, while I use integration for N=10^8 (because WolframAlpha ran out of computation time for discrete summation for N=10^8.) I couldn't compute the sizes for N=10^9 and beyond because WolframAlpha ran out of computation time.

N Size
1 1
10 27
100 7 KB
10^3 1 MB
10^4 170 MB
10^5 20 GB
10^6 2.7 TB
10^7 320 TB
10^8 37 PB

Wednesday, November 20, 2013

Javascript crop zeroes from end of number

/*
maximum number of significant digits: 3
sample input->output:
0.150 -> 0.15, 0.100 -> 0.1, 0.0500 -> 0.05
*/

function cropZeroes3(n){

if(typeof(n) !== "number")
{
n = Number(n);
}
n = n.toPrecision(3); //Number becomes String
if((n[n.length-1] === '0') && (n.indexOf('.') !== '-1') && (Number(n) !== 0))
{
n = n.substring(0, n.length - 1);
n = Number(n);
var n = n.toPrecision(2); //Number becomes String
if((n[n.length-1] === '0') && (n.indexOf('.') !== '-1') && (Number(n) !== 0))
{
n = n.substring(0, n.length - 1);
n = Number(n);
var n = n.toPrecision(1); //Number becomes String
if((n[n.length-1] === '0') && (n.indexOf('.') !== '-1') && (Number(n) !== 0))//this might not be needed. delete this if not necessary.
{
n = n.substring(0, n.length - 1);
}
}
}

return Number(n);
}
cropZeroes3(0.150); //outputs 0.15