On swapping the order of a summation Consider the following sum $$S=\sum_{k=1}^nd(k)$$
where $d(k)$ is the number of divisors of $k$. We can rewrite the sum like this $$S=\sum_{k=1}^n\sum_{d\mid k}^k1$$
but now how can I change the order of summation? we know $d\mid k$ and $k\le n \implies d\le n$. Now what? I'm generaly bad at swapping summations
 A: Let's look at an example.  Say $n = 12$.  Then for $k \in \{1, \ldots, 12\}$, we look at set of divisors of $k$:
$$\begin{array}{c|l|c}
k & d & d(k)\\
\hline
1 & \{1 \} & 1 \\
2 & \{1, 2 \} & 2\\
3 & \{1, 3 \} & 2 \\
4 & \{1, 2, 4 \} & 3 \\
5 & \{1, 5 \} & 2 \\
6 & \{1, 2, 3, 6 \} & 4 \\
7 & \{1, 7 \} & 2 \\
8 & \{1, 2, 4, 8 \} & 4 \\
9 & \{1, 3, 9 \} & 3 \\
10 & \{1, 2, 5, 10 \} & 4 \\
11 & \{1, 11 \} & 2 \\
12 & \{1, 2, 3, 4, 6, 12 \} & 6
\end{array}$$
Now we want the sum to look like this:
$$\sum_{d=1}^n \sum_{k \in ?} 1$$
So for $d = 1$, we need to count the inner summand a total of $12$ times, one for each row in the table.  But for $d = 2$, we count only $6$ times, corresponding to $k \in \{2, 4, 6, 8, 10, 12\}$.  Similarly, for $d = 3$, we count $4$ times, for $k \in \{3, 6, 9, 12\}$.  And now the pattern should be clear:  for each $d$, we count a total of $\lfloor n/d \rfloor$ times, for $$k \in \{d, 2d, \ldots, \lfloor n/d \rfloor d \}.$$  So the desired double sum might look like this:
$$S = \sum_{k=1}^n d(k) = \sum_{k=1}^n \sum_{d \mid k} 1 = \sum_{d=1}^n \sum_{k = 1}^{\lfloor n/d \rfloor} 1 = \sum_{d=1}^n \left\lfloor \frac{n}{d} \right\rfloor.$$
A: In my opinion, the best way (for me anyway) to tackle this problem is to take off my shoes, and stretch my intuition with a specific example.
Suppose that $n = 6$, and you compute
$$\sum_{k=1}^6 \sum_{d|k} 1 = 1 + 2 + 2 + 3 + 2 + 4 = 14. \tag1 $$
In (1) above:

*

*the factor of $(1)$ is counted $6$ times.

*the factor of $(2)$ is counted $(3)$ times.

*the factor of $(3)$ is counted $(2)$ times.

*the factors of the numbers $(4), (5),$ and $(6)$ are each counted once.

For any real number $r$, let $f(r)$ denote $\lfloor r\rfloor$ (i.e. the floor of $r$), which is the largest integer $\leq r$.
So, you can (in effect) reverse the order of summation by expressing it as
$$\sum_{d=1}^n \sum_{k=1}^{f\left(\frac{n}{d}\right)} 1. \tag2 $$
The reason that (2) above represents a reversal of the order of summation of (1) above is that in (2) above, the outer summation represents the potential divisors, while the inner summation represents the upper bound of the number of times that a specific divisor should be counted.
A: Let's make a table, the rows and columns are $1,2,3...,n$ and an element $(d,k)$ is $1$ if $d\mid k$ and $0$ otherwise,
\begin{array} {|r|r|}\hline  & k & 1 & 2 & 3 & 4 & 5 \\ \hline d & * & * & * & * & * & * \\ \hline 1 & * & 1 & 1 & 1 & 1 & 1 \\ \hline 2 & * & 0 & 1 & 0 & 1 & 0 \\ \hline 3 & * & 0 & 0 & 1 & 0 & 0 \\ \hline 4 & * & 0 & 0 & 0 & 1 & 0 \\ \hline \end{array}
In the original sum, we fix a $k$ and count the divisors of $k$, so if we want to swap the summation we would fix a $d$ and count how many $k\le n$ are there such that $d\mid k$? The answer is $\lfloor n/d \rfloor $
Hence $$\sum_{k=1}^nd(k)=\sum_{k=1}^n\sum_{d\mid k}^k1=\sum_{d=1}^n\sum_{k\le n\\ d\mid k}=\sum_{d=1}^n \left\lfloor \frac{n}{d} \right\rfloor $$
A: Here we change the order of summation by transformations of the sums only.

We obtain
\begin{align*}
\color{blue}{\sum_{k=1}^nd(k)}&=\sum_{k=1}^n\sum_{{d=1}\atop{d\mid k}}^k1
=\sum_{{1\leq d\leq k\leq n}\atop {d\mid k}}1\tag{1}\\
&=\sum_{d=1}^n\sum_{{k=d}\atop{d\mid k}}^n 1\tag{2}\\
&=\sum_{d=1}^n\sum_{{k=d}\atop {dd^{\prime}=k}}^n1\tag{3}\\
&=\sum_{d=1}^n\sum_{d^{\prime}=1}^{\left\lfloor n/d\right\rfloor} 1\tag{4}\\
&\,\,\color{blue}{=\sum_{d=1}^n\left\lfloor\frac{n}{d}\right\rfloor}\tag{5}
\end{align*}

Comment:

*

*In (1) we write the index range somewhat more conveniently.


*In (2) we change the order of summation.


*In (3) we introduce $d^\prime$ using the definition of the divisor $d$.


*In (4) we sum over $d^\prime$ instead of $k$. We observe $d^\prime=1$ if $k=d$, $d^\prime=2$ if $k=2d$, etc.


*In (5) we make a final simplification.
This is a special case of the following identity
\begin{align*}
\color{blue}{\sum_{k=1}^n\sum_{d\mid k}f(d,k)=\sum_{d=1}^n\sum_{k=1}^{\left\lfloor n/d\right\rfloor}f(d,kd)}
\end{align*}
which is shown in this answer.
