# On the sum of digits of $2^x$

I was looking into the sum of the digits of $$2^x$$. When I plotted y = sum of digits of $$2^x$$, the relationship was very linear (as one should expect) with a correlation coefficient of $$0.999587$$, when I calculated to $$x = 10000$$. However, my question concerns the slope of the line of best fit: $$1.3556600856084817$$. The slope seemed to fluctuate around $$1.35$$. Is there any reason for this?

Here are the slopes for f(n), representing the slope of y = the sum of digits for n^x: When plotted, it appears to be logarithmic.

• f(2) = 1.3556600856084817
• f(3) = 2.1458633077830696
• f(4) = 2.709191384919649
• f(5) = 3.147413019705652
• f(6) = 3.5012234023042232
• f(7) = 3.8034815099113444
• f(8) = 4.064013020137782
• (9) = 4.293926142384194
• f(11) = 4.68565515673792
• f(12) = 4.856282415979148
• f(13) = 5.0120684909979305
• f(14) = 5.156650757838338
• f(15) = 5.29277691693154
• f(35) = 6.946651885840713
• f(99) = 8.980022856020353
• f(223) = 10.56770121473439

Does anyone have any explanation for the meaning behind these slopes?

Here is some of my sloppy python code if you want to play around with it:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

sum_npx = []

#sum of digits of n^x
def sum_of_digits(integer_list):
total_digit_sum = 0
for number in integer_list:
num_str = str(number)
for char in num_str:
if char.isdigit():
total_digit_sum += int(char)

for _ in range(1, 10000):
sum_npx.append(sum_of_digits([int(i) for i in str(2**_)]))

x = [a for a in range(1, 10000)]
plt.plot(np.unique(x), np.poly1d(np.polyfit(x, sum_npx, 1))(np.unique(x)))
plt.plot(x, sum_npx)
plt.show()

df = {
"Array 1": x,
"Array 2": sum_npx
}

coefficients = np.polyfit(x, sum_npx, 1)

# Extract the slope (coefficient for x, which is the first element)
slope = coefficients[0]

# Print the slope
print("Slope of the line of best fit:", slope)

data = pd.DataFrame(df)
print(data.corr())


• I presume you are referring to the digits of 2^x in base 10. Nov 20 at 20:55
• Yes, in base 10. Nov 20 at 20:57
• Not sure if it's relevant, but I immediately thought of Benford's Law. en.m.wikipedia.org/wiki/Benford%27s_law Nov 21 at 18:54
• @LamarLatrell Note that Benford's law implies that the distribution of the $n$-th digit, as $n$ increases, rapidly approaches a uniform distribution. Nov 21 at 20:37
• Nov 23 at 20:06

$$2^x$$ has about $$\log_{10}(2^x) = x\log_{10}(2) \approx 0.30103x$$ digits in base $$10$$. The average digit is about $$4.5$$, so the sum of the digits is about $$4.5 \log_{10}(2)x \approx 1.35 x.$$

This argument generalizes to show that the sum of the base $$b$$ digits of $$n^x$$ is about $$\frac{b-1}{2}\log_{b}(n) x$$

• You seem to be assuming that the digits in the decimal expansions of $2^x$ will be uniformly distributed. That sounds very plausible, but how do you prove it? Nov 20 at 21:32
• @RobArthan Indeed I did (unthinkingly) assume that uniform distribution. The OPs calculations provide evidence. I suspect a proof is either easy or moderately hard. I'm not actively looking for one. Nov 20 at 22:16
• @EthanBolker as far as I know, it is extremely hard — we don't even know that there isn't an infinite number of powers of 2 avoiding the digit 9. Nov 20 at 22:20
• @CommandMaster Thanks. If I could still edit my comment I'd change "moderately hard" to "extremely hard". Nov 20 at 22:24
• @Didier The parenthetical remark was not supposed to suggest that, but just to link to a different approach. Nov 21 at 16:17

It may be worth noting how well the wedge-like shape of this plot is explained by the assumption that the digits of $$2^x$$ are approximately uniformly distributed (and independent). The mean and variance of the sum of these digits in this case are easily found to be as follows: $$\mu(x)=4.5\times (\text{number of digits in 2^x})$$ $$\sigma^2(x)=8.25\times(\text{number of digits in 2^x})$$ Computations show (in the plot on the left) that the two lines $$\mu(x)\pm 3\,\sigma(x)$$ almost perfectly outline the plot, with $$\mu(x)$$ being the center line:

The plot on the right shows the approximate normality of the sums-of-digits by using their standardized values $$Z(x)={\text{sum-of-digits(2^x)}-\mu(x)\over\sigma(x)},$$ the blue curve being the standard normal density function.

SageMath code for the plot on the left:

def sum_of_digits(x, n, b):
return sum((n^x).digits(base=b))
def mu(x, n, b):
return ((b - 1) / 2) * (1 + floor(log(n^x, b)))
def sd(x, n, b):
return sqrt( ((b^2 - 1) / 12) * (1 + floor(log(n^x, b))))
b = 10
n = 2
xmax = 10^4
L = [(x, sum_of_digits(x,n,b)) for x in range(xmax)]
Lmu = [(x, mu(x,n,b)) for x in range(xmax)]
Llo = [(x, mu(x,n,b) - 3.0*sd(x,n,b)) for x in range(xmax)]
Lhi = [(x, mu(x,n,b) + 3.0*sd(x,n,b)) for x in range(xmax)]
gr = line(L, color='orange')
gr += line(Lmu, linestyle=':', color='black')
gr += line(Llo, linestyle=':', color='blue')
gr += line(Lhi, linestyle=':', color='blue')
gr.show(axes_labels=[r'Integer $$x$$', 'Sum of digits'],
axes_labels_size=1, frame=True, axes=False,
title=r'Sum of base-$${%s}$$ digits in $${%s}^x$$ versus $$x$$'%(str(b),str(n)), figsize=4)

• +1 definitely worth noting. Nov 22 at 0:36
• I wonder whether what seems to be a very slight skew in the histogram is a real second order effect. What happens if you sample between the integers and use the sum of the digits of the integral part of the power? Nov 22 at 17:04
• That's an interesting idea -- I'll check it out. For now, I notice that the slight "shoulder" on the right in the histogram seems to be an effect that vanishes as sample size increases -- at $10^5$ it's still just barely visible. Nov 22 at 17:24
• Using $\lfloor n^x\rfloor$ with $x$ varying in fractional step-sizes, I find that (as expected) the skewness & kurtosis measures of the $Z$-sample both decrease towards $0$ with increasing sample size. Curiously, sampling with (x=1..10^4,step=0.1) produces measurably less skew and kurtosis than does (x=1..10^5,step=1), although both have sample size 10^5; and both of those are of course significantly less than with (x=1..10^4,step=1). The (mean, variance, skewness, kurtosis) for these three cases are, respectively, (-.03, 1.004,-.002,-.007),(-.008,1.000,-.009,-.01), and (.01,1.003,-.01,-.1). Nov 22 at 19:10