0

I'm aware that $D(n)$ can be calculated in O(sqrt(n)) time. Can $ D(n, k) $ also be calculated in O(sqrt(n)) time? What's the best algorithm?

For example, if $n = 8$ and $k = 3$, then $ D(n, k) = \lfloor{\frac{8}{1}}\rfloor + \lfloor{\frac{8}{2}}\rfloor + \lfloor{\frac{8}{3}}\rfloor $

2 Answers2

1

You meant $$\sum_{a,b, ab\le n, a \le k}1 = \sum_{a \le k} \left\lfloor \frac{n}{a} \right\rfloor$$

Then yes it can be computed in $O(\sqrt{n})$ operations because $$\left\{\left\lfloor \frac{n}{a} \right\rfloor, a \ge 1\right\}$$ contains at most $2 \sqrt{n}+1$ elements and

$\left\lfloor \frac{n}{a} \right\rfloor = c \iff \frac{n}{a} = c+r, r \in [0,1)$

$\iff n= ca+ra, a =\frac{n}{c+r}$

$\iff a \in (\frac{n}{c+1},\frac{n}{c}]$ so $\left\lfloor \frac{n}{c}\right\rfloor-\left\lfloor \frac{n}{c+1}\right\rfloor$ many choices for $a$

This way we obtain $$ \sum_{a \le k} \left\lfloor \frac{n}{a} \right\rfloor = \sum_{a =1}^{ \min(\lfloor \sqrt{n}\rfloor,k)} \left\lfloor \frac{n}{a} \right\rfloor+ \sum_{c=\lfloor n/k\rfloor}^{\lfloor n/\lfloor \sqrt{n}\rfloor\rfloor-1} \min(k,\left\lfloor \frac{n}{c}\right\rfloor)-\min(k,\left\lfloor \frac{n}{c+1}\right\rfloor)$$

reuns
  • 77,999
  • Okay, but how do I calculate it in O(sqrt(n)) time? – bifurcate870 Sep 15 '19 at 16:06
  • Find the intervals where $\left\lfloor \frac{n}{a} \right\rfloor$ is constant $=c$ – reuns Sep 15 '19 at 16:07
  • Can you explain using an example? – bifurcate870 Sep 15 '19 at 16:08
  • It'd be good if you could write down the algorithm. – bifurcate870 Sep 15 '19 at 16:09
  • It's not difficult $\frac{n}{a}=c+r, r \in [0,1) \implies a=...$ – reuns Sep 15 '19 at 16:09
  • How do you determine the constant elements and frequency of each such element? It is a little unclear. – bifurcate870 Sep 15 '19 at 16:28
  • $n= ca+ra, a(1+\frac{r}{c}) = \frac{n}{c},r \in [0,1), a \in (\frac{\frac{n}{c}}{1+\frac{1}{c}},\frac{n}{c}]$ so $\left\lfloor \frac{n}{c}\right\rfloor-\left\lfloor \frac{\frac{n}{c}}{1+\frac{1}{c}} \right\rfloor$ many choices for $a$ – reuns Sep 15 '19 at 16:35
  • I'm really sorry, but I'm not getting it. Let's say I have n = 18 and k = 8. Then how are you solving it? Can you show it step by step? – bifurcate870 Sep 15 '19 at 16:44
  • Enumerate the $c$ instead of the $a$... – reuns Sep 15 '19 at 16:45
  • I got that, now just tell me how are you finding all the 'c's in O(sqrt(n)) time? – bifurcate870 Sep 15 '19 at 16:53
  • For $a \le \sqrt{n}$ we do enumerate the $a$, for $a > \sqrt{n}$ we do enumerate the $c$ not the $a$ – reuns Sep 15 '19 at 16:56
  • For n = 18 and k = 8, you're saying that a is less than or equal to sqrt(18) = 4. But if k = 8, a = 6 and a = 7 and a = 8 are valid but they're not <= 4. Maybe I'm missing something? Mind explaining this particular case? – bifurcate870 Sep 15 '19 at 17:16
  • That's not what I said. $c=\left\lfloor \frac{n}{a} \right\rfloor$ depending on the size it's more efficient to enumerate the $a$ or the $c$. For example do you see that $c$ is constant $=1$ on $a \in (n/2,n]$ ? So no need to enumerate all the $a$ in that large interval. – reuns Sep 15 '19 at 17:24
  • I know that I'm bothering you too much, but can you explain the entire thing with n = 18 and k = 8? A well-explained example will clear the thing completely. – bifurcate870 Sep 15 '19 at 17:29
  • Your last edit made it crystal clear. Thanks! – bifurcate870 Sep 16 '19 at 17:35
0

Yes you can, I also have a problem that require this function to be calculated fast enough.

You can notice that there is $O(\sqrt{n})$ unique values in the set S = {$\lfloor \frac{n}{1} \rfloor, \lfloor \frac{n}{2} \rfloor, \lfloor \frac{n}{3} \rfloor, \dots, \lfloor \frac{n}{n - 1} \rfloor, \lfloor \frac{n}{n} \rfloor$}. Therefore you can calculate the function in $O(\sqrt{n})$, even with partial divisor summatory function.

Even more complex but faster: using the method that Richard Sladkey described in this paper you can calculate the function in $O(n^{1/3})$