You want to calculate
n! ~ sqrt(2*pi*n) * (n/e)^n (Stirling's approximation)
but it is too big when n>170, so what do you do?
I also worked out the following answer, which is to figure out the integer base-10 exponent, and the corresponding mantissa, using the standard rules for powers and logarithms:
M2 = M1*10^(E1-floor(E1)); E2=floor(n*log10(n/exp(1)));
while M2>10, M2=M2/10; E2=E2+1; end
fprintf('n! ~= %.3fx10^%d\n',M2,E2);
Line 1 computes the mantissa and base-10 exponent, but the exponent is generally not an integer.
Line 2 makes the exponent an integer and adjust the mantissa accoridngly.
Line 3 divides the mantissa by 10, and raises the exponent by 1, repeatedly, until the mantissa is <10.
Line 4 prints the result.
When you compare the result above to the actual factorial, for 10<=n<=170, you see that the Stirling approximation is good, and gets better as n gets bigger. For n=1000, the above code gives
n! ~= 4.024x10^2567