(This task is taken from a   Project Euler   problem.)

(All numbers herein are expressed in base ten.)


27   =   128   and   7   is
the first power of   2   whose leading decimal digits are   12.

The next power of   2   whose leading decimal digits
are   12   is   80,

280   =   1208925819614629174706176.



Define        p(L,n)      to be the  nth-smallest
value of    j    such that the base ten representation
of    2j    begins with the digits of    L .

    So   p(12, 1) =  7    and
         p(12, 2) = 80



You are also given that:

         p(123, 45)   =   12710



Task

   find: 
     p(12, 1)  
     p(12, 2)  
     p(123, 45)  
     p(123, 12345)  
     p(123, 678910)  
   display the results here, on this page.


function p(L, n)
    @assert(L > 0 && n > 0)
    places, logof2, nfound = trunc(log(10, L)), log(10, 2), 0
    for i in 1:typemax(Int)
        if L == trunc(10^(((i * logof2) % 1) + places)) && (nfound += 1) == n
            return i
        end
    end
end
 
for (L, n) in [(12, 1), (12, 2), (123, 45), (123, 12345), (123, 678910)]
    println("With L = $L and n = $n, p(L, n) = ", p(L, n))
end
 