---
# System prepended metadata

title: Tổng n phép chia

---

# Tổng n phép chia
Tính
$S = \left [ \frac{N}{1} \right ] + \left [ \frac{N}{2} \right ]+...+\left [ \frac{N}{N-1} \right ]+\left [ \frac{N}{N} \right ]$

Công thức:

$S= 2\left (  \left [ \frac{N}{1} \right ] + \left [ \frac{N}{2} \right ]+...+\left [ \frac{N}{k} \right ]  \right ) - k^2$

Với $k = [\sqrt N]$

hoclen.top

C++

```c++
#include <bits/stdc++.h>
#define lli long long int
using namespace std;
int main()
{
	int n;
	cin >> n;
	int k=sqrt(n);
	lli s=0;
	for (int i=1; i<=k; i++) s+=n/i;
	s=2*s-k*k;
	cout<<s;
	return 0;
}
```
Pascal
```delphi
var n,i:longint;
      k:integer;
      s:int64;
begin
    readln(n); 
    k:=trunc(sqrt(n));
    s:=0;
    for i:=1 to k do
        s:= s + n div i;
    s:= 2*s - sqr(k);
    write(s);
end.
```
