Sunday, May 26, 2013

Mental Math II: Calculate logarithm, then power

So me was standing on street doing some boring work, and came up with the wild idea of calculating the old question 1.76^893 again. I realise squaring it 9 times was too erroneous. Why not log. By log I mean log base 10, lg mean log base 2.

First log 1.76 = (log 176) - 2 = (lg 176 / lg 10) - 2.
I convert everything to base two, since it is the smallest natural base.
lg 10 = (1/3) lg 1000 ~ (1/3) lg 1024 = 10/3. Thus lg 10 ~ 3.3
lg 176 = lg 11 + lg 16 = 4 + lg 11.
lg 11 = (1/2) lg 121 ~ (1/2) lg 128 = 7/2 = 3.5
So lg 176 = 7.5, implying lg 176 / lg 10 = 7.5/3.3 = 7.5*3 /10 = 2.25.
Thus log 1.76 = 0.25. Thus log 1,76^893 = 893/4 = 223.

So the 1.76^893 should have like 220 digits. Much better estimate.
Conclusion, to calculate log we convert everything to base two, since there are a lot of powers of two around. Let say we want to calculate lg 7, we find square or cube, etc. of 7 that is closed to a power of 2. 7^2 = 49, not really, 7^3 = 343, not really. 7^4 = 49^2 = 50*48+1 = 2401 quite close to 2048. So log (7^4) = a bit bigger than 11, say 11.2. So lg 7 is 11.2/4 = 2.8. Damn I think I cheated it is 2.807. So accurate.

So in conclusion, to do all kind of mental math, all you need to be good at is multiplication, especially squaring two digit number. The rest gonna be easy.

Now question, how many digits are there in 100!?

Alright this is me doing it in real-time. I'm gonna approximate sum of log from 10 to 100. 10^1.5 = 31.xx. So (log 10 + ... + log 31)  ~ 1 + .... + 1.5. It should be a bit bigger than (1+1.5)*11 = 27.5

log 32 + ... + log 100 ~ (1.5 + 2) * 69/2 ~ 0.1 * 35^2 = 122.5. So the total is 150. Now 9! we can calculate. 5! is 120. 6! 720 -> 5040 -> 40000 -> 360000. So 5.5 more digits. So should be 155.5.
As we underestimate the sum of log (because log is concave). Let's round this number up say 160.

Let's check the answer. 9*10^157. Damn sweet. Not bad.