9

How do I get the leading digits for a large power of 2 (say $2^{123456789}$, although I want a general method)? I was thinking about repeatedly dividing it by ten, but I don't know how to do that efficiently without calculating the number itself, and I was also thinking $\log(a) < 123456789\log(2) < b$ would imply that it would be the first three digits of $a$, but I don't know how I would find $a$ and $b$ in the first place.

b_pcakes
  • 1,501
  • I sincerely doubt that there is any efficient way to do it (more efficient than calculating the decimal representation of the number). – barak manos Nov 09 '16 at 05:21

2 Answers2

17

Figured it out! We first need to find the number of digits of $2^n$, which can be done using logarithms: we need to solve (approximately) $2^n = 10^d$. In the end, the formula for the number of digits $d$ is $d = 1+\left\lfloor n\,\log_{10}2\right\rfloor$. Then, once we have $d$, then we see that if we can solve for $10^{d-k} \cdot t < 2^n < 10^{d-k} \cdot (t + 1)$ (where $k$ is the number of leading digits we want), then $t$ is the number we want. In this specific example, we find that the first three digits are $\lfloor 10 ^ {n \log2 - d + 3}\rfloor = 454$, where $n = 123456789$ and $d$ is calculated as above.

b_pcakes
  • 1,501
  • I hope you're aware of the fact that $d$ is not integer. 2. What is $a$? 3. How exactly does that get you the leading digits (and how many of them does it get you)???
  • – barak manos Nov 09 '16 at 05:37
  • I was in the process of editing the answer, it is still "under construction" – b_pcakes Nov 09 '16 at 05:38
  • @barakmanos I edited the answer to be more clear. I hope it is clear why we use that inequality (since we only care about the first $k$ digits). – b_pcakes Nov 09 '16 at 05:51
  • Sounds to me like you still need to calculate $2^n$ in order to make that comparison. – barak manos Nov 09 '16 at 06:00
  • @barakmanos Of course not. We can avoid it because we take the log of all three sides of the inequality. It is easy to see how the inequality can be solved for $n$ ( recall that $\log a^b = b \log a$ and $\log(ab) = \log(a) + \log(b)$). In other words we can solve for $t$ directly. – b_pcakes Nov 09 '16 at 06:26
  • I cannot see how you would calculate $t$ without calculating $\frac{2^n}{10^{d-k}}$ (for which, you first need to calculate $2^n$). Perhaps you can show, for example, how you calculate the first $5$ digits of $2^{80}$. – barak manos Nov 09 '16 at 06:28
  • 1
    @barakmanos $\log(10^{d - k}\cdot t) < \log(2^n) < \log(10^{d-k}\cdot (t+1))$, which is equivalent to $d-k + \log t < n\log(2) <d-k + \log(t+1)$. I hope you can see how to proceed from here. If not, we can continue this in chat. – b_pcakes Nov 09 '16 at 06:32
  • Can you show an example or not? – barak manos Nov 09 '16 at 06:33
  • No need to. If you cannot show an example, then the only conclusion is that your method doesn't achieve what you claim. – barak manos Nov 09 '16 at 06:34
  • The example is in my answer. Have you read it? – b_pcakes Nov 09 '16 at 06:35
  • Yes, but I have no idea what the leading $3$ digits of $2^{123456789}$ are in order to confirm that (which is why I suggested the example of $2^{80}$ instead). – barak manos Nov 09 '16 at 06:41
  • 1
    @barakmanos Okay, then according to my formula we have $n = 80$, $k = 5$, $d = 1 + \lfloor n \log 2\rfloor = 25$, and the answer is $\lfloor 10 ^ {n \log2 - d + k}\rfloor = 12089$. If you still have doubts I really suggest we continue this in chat. – b_pcakes Nov 09 '16 at 06:43
  • 3
    OK, I've verified this with a Python script, very nice!!! I think that you just need to write it down explicitly as $\lfloor{10^{n\log2-\lfloor{n\log2}\rfloor+k-1}}\rfloor$, then accept your own answer. +1 from me, of course... – barak manos Nov 09 '16 at 06:49
  • 1
    Note that, if this is done with floating-point numbers, you lose as many bits of precision as $n$ has. Regular IEEE-754 64-bit floats (doubles) have a 52-bit mantissa, so the usable bits of mantissa you'll get is approximately $52-log_2(n)$ (e.g. for $2^{100000}$ you'll get roughly 35 bits of precision) – dzaima Oct 30 '21 at 17:53