<p>You can notice that there is O(n^(1/2)) unique values in the set S = {⌊n/1⌋, ⌊n/2⌋, ..., ⌊n/(n-1)⌋, ⌊n/n⌋}. Therefore you can calculate the function in O(n^(1/2))</p>
<p>Also since this function is asymmetric, you can even calculate x2 faster by using this formula: D(n) = Σ(x=1-&gt;u)(⌊n/x⌋) - u^2 for u = ⌊n^(1/2)⌋</p>
<p>Even more complex but faster: using the method that Richard Sladkey described in <a href="https://arxiv.org/abs/1206.3369" rel="nofollow noreferrer">this paper</a> you can calculate the function in O(n^(1/3))</p>
How to sum sequence?
<p>How can I sum the following sequence:</p>

<pre><code>⌊n/1⌋ + ⌊n/2⌋ + ⌊n/3⌋ + ... + ⌊n/n⌋
</code></pre>

<p>This is simply O(n) solution on C++:</p>

<pre><code>#include &lt;iostream&gt;
int main()
{
   int n;
   std::cin&gt;&gt;n;
   unsigned long long res=0;
   for (int i=1;i&lt;=n;i++)
   {
      res+= n/i;
   }
   std::cout&lt;&lt;res&lt;&lt;std::endl;
   return 0;
}
</code></pre>

<p>Do you know any better solution than this? I mean O(1) or O(log(n)). Thank you for your time :) and solutions</p>

<p>Edit:
Thank you for all your answers. If someone wants the solution O(sqrt(n)):
Python:</p>

<pre class="lang-py prettyprint-override"><code>import math
def seq_sum(n):
 sqrtn = int(math.sqrt(n))
 return sum(n // k for k in range(1, sqrtn + 1)) * 2 - sqrtn ** 2
n = int(input())
print(seq_sum(n))
</code></pre>

<p>C++:</p>

<pre class="lang-c prettyprint-override"><code>#include &lt;iostream&gt;
#include &lt;cmath&gt;
int main()
{
   int n;
   std::cin&gt;&gt;n;
   int sqrtn = (int)(std::sqrt(n));
   long long res2 = 0;
   for (int i=1;i&lt;=sqrtn;i++)
   {
      res2 +=2*(n/i);
   }
   res2 -= sqrtn*sqrtn;
   std::cout&lt;&lt;res2&lt;&lt;std::endl;
   return 0;
}
</code></pre>