calculate elements of harmonic floor progression

71 Views Asked by At

I need to calculate the distinct elements which contribute to the sum

$\sum_{i=1}^n \lfloor \frac{n}{i}\rfloor$

where $n = 10^{2m}$ and $m$ is a natural number.

For a given $n = 10^{2m}$ it results that there are $2 \cdot 10^m - 1$ distinct elements which contribute to that sum. E.g. for $n = 100$, the distinct value set is $V(n) = \{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 20, 25, 33, 50, 100\}$ which counts 19 elements. How can I precalculate all the values of the set $V$? I feel there is an easy $O(\sqrt{n})$ (or however faster than $O(n)$) algorithm to do that but I can't see it now...

1

There are 1 best solutions below

2
On

Solved on my own. No need of an algorithm:

$V(n) = \{i, i = 1, 2, \ldots, \lfloor \sqrt{n} \rfloor \} \cup \{ \lfloor \frac{n}{i} \rfloor , i = 1, 2, \ldots, \lfloor \sqrt{n} \rfloor - 1 \}$

for $n = 10^{2m}, m \in \mathbb{N}$.