6
$\begingroup$

In a few programming contexts, I've come across code along these lines:

total = 0
for i from 1 to n
    total := total + n / i

Where the division here is integer division. Mathematically, this boils down to evaluating

$$\sum_{i = 1}^{n}\left\lfloor\, n \over i\,\right\rfloor.$$

This is upper-bounded by $n H_{n}$ and lower-bounded by $nH_{n} - n$ using the standard inequalities for $\texttt{floor}$'s, but that upper bound likely has a large gap to the true value.

Is there a way to either get an exact value for this summation or find some simpler function it's asymptotically equivalent to?

$\endgroup$
6
  • 1
    $\begingroup$ Should "contexts" be "contests"? $\endgroup$
    – hardmath
    Commented Aug 20, 2016 at 0:05
  • $\begingroup$ Actually, no! I was wondering if anyone would ask that. :-) $\endgroup$ Commented Aug 20, 2016 at 0:06
  • 1
    $\begingroup$ The upper bound is $nH_n$ not $H_n$. (And the lower bound is correspondingly $n(H_n-1)$, which is at least good enough for asymptotics) $\endgroup$ Commented Aug 20, 2016 at 0:09
  • $\begingroup$ Oh whoops! Let me fix that. $\endgroup$ Commented Aug 20, 2016 at 0:10
  • 3
    $\begingroup$ This is known as divisor summatory function. It can be computed in $O(\sqrt{x})$ time. Look at the wiki page for refs and more details. $\endgroup$ Commented Aug 20, 2016 at 0:24

3 Answers 3

8
$\begingroup$

For exact computation, a simplification begins with the observation

$$ \sum_{i=1}^n \left\lfloor \frac{n}{i} \right\rfloor = \sum_{i,j} [1 \leq i] [1 \leq j] [ij \leq n] 1$$

where $[P]$ is the Iverson bracket: it is $1$ if $P$ is true, and $0$ if false.

Anyways, using the symmetry, you can break this down to

$$ \sum_{i=1}^n \left\lfloor \frac{n}{i} \right\rfloor = -\lfloor \sqrt{n} \rfloor^2 + 2 \sum_{i=1}^{\lfloor \sqrt{n} \rfloor} \left\lfloor \frac{n}{i} \right\rfloor $$

This form is better for making rough estimates too, since the roundoff error is a much smaller proportion of the summands.

$\endgroup$
2
  • 1
    $\begingroup$ This is also known as dirichlet's hyperbola method $\endgroup$
    – Asinomás
    Commented Aug 20, 2016 at 0:37
  • 1
    $\begingroup$ I write it $\sum_{i \ge 1,j \ge 1} 1_{ij \ge n}$ $\endgroup$
    – reuns
    Commented Aug 20, 2016 at 6:59
5
$\begingroup$

See OES sequence A006218 and Dirichlet divisor problem. Asymptotically we have $$a(n) = n (\log n + 2 \gamma - 1) + O(n^\theta)$$ where the optimal value of $\theta$ is an unsolved problem: the best result given on that MathWorld page is $\theta = 131/416$, due to Huxley in 2003.

$\endgroup$
4
$\begingroup$

Your sum is also equal to $\sum_{i=1}^n \tau(i)$. Notice that if we were able to calculate this sum really fast we would also be able to determine whether $n$ is prime really fast, so calculating your sum is not very easy.

$\endgroup$
5
  • $\begingroup$ Is that supposed to be $\tau(i)?$ $\endgroup$ Commented Aug 20, 2016 at 0:11
  • $\begingroup$ yeah, my bad.${}{}{}{}$ $\endgroup$
    – Asinomás
    Commented Aug 20, 2016 at 0:12
  • $\begingroup$ What is the function $\tau(i)$ here? Number of divisors of $i$? $\endgroup$
    – MT_
    Commented Aug 20, 2016 at 0:18
  • $\begingroup$ Perhaps I'm missing something, but how would this help you tell if $n$ were prime? $\endgroup$ Commented Aug 20, 2016 at 0:20
  • 6
    $\begingroup$ $\sum_{i=0}^{n-1} \tau(i)+2 =\sum_{i=0}^{n}\tau(i)\iff n$ is prime $\endgroup$
    – Asinomás
    Commented Aug 20, 2016 at 0:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .