#!/usr/bin/env python ### log2_factorial -- Use Stirling's formula to estimate log2( n! ). # See my factorial.py. sw_at_not_this_tiac_dot_net # # For 0 <= n <= 170, this uses the actual factorial and is accurate to within # the accuracy of the log() function. # For 171 <= n <= 1e7, I've tested it and the answer seems accurate to within # +1e-3. That's a number of bits, so, pretty good. from factorial import * from sys import argv if argv[1] == "-d": print defact( int( argv[2] ) ) elif argv[1] == "-b": print log2_factorial( int( argv[ 2 ] ) ) / 8, "bytes" elif argv[1] == "-f": print factorial( int( argv[ 2 ] ) ) elif argv[1] == "-p": print pow( 2, float( argv[ 2 ] ) ) else: print log2_factorial( int( argv[ 1 ] ) )