From the problem...

Find the minimal positive integer $b$ such that the first digits of $2^b$ are 2011

...I have been able to reduce the problem to the following instead:

Find minimal $b$ such that $\log_{10} (2.011) \leq \operatorname{frac}(b~\log_{10} (2)) < \log_{10} (2.012)$, where $b$ is a positive integer

Is there an algorithm that can be applied to solve this or would you need to step through all possible b until you find the right solution?

  • 1
    $\begingroup$ This type of problem could be viewed as the problem of finding efficient (smallish denominator, small error) rational approximations to certain irrationals. The most standard tool for this is continued fractions. But now that computation is fast and incredibly cheap, brute force has a certain appeal. $\endgroup$ – André Nicolas Jun 18 '11 at 17:27
  • $\begingroup$ Using continued fractions is convenient only with computation anyway, and with computation, using continued fractions gives faster solutions. (E.g. if instead of "2011" we wanted a power of 2 that begins with a longer string like "12345678", then brute force would not be fast enough.) $\endgroup$ – ShreevatsaR Jun 19 '11 at 16:33

This is an elaboration of Gerry Myerson's answer (and user6312's comment).

As you derived, we want to find an integer $b$ such that for some integer $k$, $$(2.011)10^k < 2^b < (2.012)10^k$$ which, taking logarithms to base 10 etc., is equivalent to ($\{x\}$ denotes the fractional part of $x$): $$\log_{10}{2.011} < \{b~\log_{10}{2}\} < \log_{10}{2.012}.$$

Because $\log_{10}{2}$ is irrational, the sequence $\{b~\log_{10}{2}\}$ (for $b$ varying over positive integers) is dense in $[0,1]$, so you are guaranteed that there are infinitely many $b$ for which $\{b~\log_{10}{2}\}$ lies in the interval $(\log_{10}2.011, \log_{10}2.012)$. In fact, the sequence is also uniformly distributed in the unit interval, so that $\log_{10}2.012 - \log_{10}2.011 \approx 0.0002159$ is the fraction of integers with this property, and indeed a computer search finds 21 such integers in the first 100000 and 216 in the first 1000000.

Given an irrational number $\theta$, issues about making $\{q\theta\}$ close to $0$ (for integer $q$) are called homogeneous diophantine approximation; making it close to some arbitrary number $\alpha$ is called inhomogeneous diophantine approximation, which is what we have here.

Here is one way, possibly not the simplest, to find such $q$ (based on Module 16 of Edward Burger's Exploring the Number Jungle, a great book):

  • Using the continued fraction of $\theta$ etc., find relatively prime integers $m$ and $n$ such that $|\theta n - m| < \frac1n$.

  • Let $N$ be the closest integer to $\alpha n$ (so that $|\alpha n - N| \le \frac12$).

  • Write $N$ as $vm - un$ with $|v| \leq \frac{n}2$ (using Euclidean algorithm etc.)

  • Then, for $q = n + v$ and $p = m + u$, we have $$ |\theta q - p - \alpha| < \frac{3}{q} $$

Proving all this (after the first step) is just some elementary algebra. For our problem with $\theta = \log_{10}2$ and $\alpha = \log_{10}2.011$, the first convergent that is good enough is $m/n = 643/2136$. Then $N$ is the closest integer to $n\alpha = 2136\alpha$ so $N = 648$. We write it as $N = v(643) - u(2136)$ with $v = -288$ and $u = -87$. So $q = n + v = 2136 - 288 = 1848$ and $p = m + u = 643 - 87 = 556$. And indeed, we have $1848\log_{10}2 \approx 556.30343$ and its fractional part lies between $\log_{10}{2.011} \approx 0.30341$ and $\log_{10}{2.012} \approx 0.30363$.

  • $\begingroup$ I just tried it out with the convergent $643/2136$, so $N = 648$ and $648 = 1848 \times 632 - 556 \times 2136$, giving $$q = 1848 + 2136 = 3984$$ and $$p = 643 + 556 = 1199$$ The funny thing is that this is actually the second solution, but the first solution (1848) appears in the working. Thanks for the reference and explanation :) $\endgroup$ – Sp3000 Jun 19 '11 at 12:18
  • $\begingroup$ @Sp3000: Interesting. Actually, you missed ensuring that $|v| \leq n/2$. I've updated my answer with what happens with that convergent; it gives the first solution 1848. It is interesting that you still got a working answer. $\endgroup$ – ShreevatsaR Jun 19 '11 at 12:45
  • $\begingroup$ @Sp3000: BTW, if you take $v$ which does not satisfy $|v| \le n/2$, then you won't get $3$ but instead (I think) something like $2 + \frac{2|v|}{n}$, so if $q$ is large enough it can still be ok. In fact, I guess this is how the other solutions (not given by the algorithm above) are generated. $\endgroup$ – ShreevatsaR Jun 19 '11 at 16:42

Here is a shotgun way to do it. This is not elegant, but it worked.

def foo(x):
    x = str(x)
    if len(x) < 4:
        return x
    return x[:4]
for k in range(1,10000):
    q = 2**k
    q = foo(q)
    if foo(q) == "2011":
        print k

I got 1848, 3984, 6120 in this little search. So your first integer is 1848.

  • $\begingroup$ This is brute forcing the problem: not a great solution. $\endgroup$ – Agos Jun 18 '11 at 15:55
  • $\begingroup$ Source of problem? A programming text? $\endgroup$ – GEdgar Jun 18 '11 at 16:42
  • $\begingroup$ I don't know. I was just looking for any solution. Once you have that, you can try some fancier analytical trickery. $\endgroup$ – ncmathsadist Jun 18 '11 at 17:01
  • 4
    $\begingroup$ Usually my objective is to minimize the time to solution, including both programming and execution time. This looks like it took little time to program, and with only 10,000 loops must have run instantly. What's wrong with that? $\endgroup$ – Ross Millikan Jun 19 '11 at 1:48
  • $\begingroup$ I could have multiplied each time then checked instead of computing the powers afresh. $\endgroup$ – ncmathsadist Jun 19 '11 at 2:28

You are looking for integers $b$ and $p$ such that $b\log_{10}2-\log_{10}(2.011)-p$ is small and positive. The general study of such things is called "inhomogeneous diophantine approximation," which search term should get you started, if you want something more analytical than a brute force search. As 6312 indicated, continued fractions come into it.

  • 1
    $\begingroup$ I tried checking the continued fractions for $log_{10}(2.011)$ and $log_{10}(2.012)$ and found that the difference of the fifth convergents, $10/33$ and $17/56$ is $1/1848$. I'm not too sure whether that is merely coincidence, or why it works if it does... $\endgroup$ – Sp3000 Jun 19 '11 at 2:55
  • $\begingroup$ The difference between consecutive convergents of a continued fraction is always one divided by the product of their denominators. $\endgroup$ – W. Cadegan-Schlieper Jun 19 '11 at 3:37
  • $\begingroup$ @WAS: That is true, but the question is whether 1848 being the same as the answer to the question (see ncmathsadist's answer) is a coincidence or not. $\endgroup$ – ShreevatsaR Jun 19 '11 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.