' Cino Hilliard, 1/31/2003 ' !3.bas BCX bignums Factorial routine. ' Program outputs the factorial of N <= 126000, and number of 2's in it. ' digits = int((n+.5)*log10(n) - n*log10(exp(1))+.3992)+1 'estimate of digits in n! ' I derived this formula by counting the digits in this program and ' Stirling's formula for N!. It is correct for N <= 100000. \$nowin #include cls dim dummy\$ dim n1\$ dim n dim n2 as long dim prec as long dim one as pBignum dim a as pBignum dim b as pBignum dim tmp as pBignum dim bigbuff[100000001] as char dim digits as long dim d[20] as long n1\$ = Command\$ print " BCX bignums Factorial." print " Use dos redirect > filename.txt to save to file" print IF n1\$ = "" THEN input "number ",n1\$ END IF n = VAL(n1\$) n2 = n/2 digits = int((n+.5)*log10(n) - n*log10(exp(1))+.3992)+1 'estimate of digits in n! print "Computing ";n;"!. Estimated digits = ";digits for n = 0 to 100 if n < 1000 then prec = digits/2 if n >= 1000 and n <= 5000 then prec = digits/7 if n > 5000 and n <= 20000 then prec = digits/8 if n > 20000 and n <= 50000 then prec = digits/7 if n > 50000 and n <= 100000 then prec = digits/4 if n > 100000 then prec = digits BignumPrecision(prec) one = newBignum(1) a = quadnew(2) b = newBignum(n2) tmp = newBignum(0) a = fact(n) quadformat(a,bigbuff) d[2] = tally(bigbuff\$,"2") print d[2]; next n print input "enter to end",dummy\$