Coding challenge

In many real-world datasets and geometric growth sequences, the leading digit $d \in \{1, \dots, 9\}$ is not equally distributed. Instead, it follows Benford's Law: $$P(d) = \log_{10}\left(1 + \frac{1}{d}\right)$$ According to this law, about $30.1\%$ of numbers should have a leading digit of $1$, while only $4.6\%$ start with $9$!

Write some code that computes powers of two from $2^1$ to $2^{1000}$. Count how many begin with the digit $1$. What percentage do you find?

(Hint: In Lua, you can compute $n \times \log_{10}(2)$ using math.log(2)/math.log(10). The fractional part $f$ gives the first digit via $\lfloor 10^f \rfloor$.)

Type your code here:


Lua reference

See your results here: