C++
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
unsigned long long int t,n,i,m,su,s,k;
int main()
{
cin>>n;
if(n==0)
{
cout<<"0\n";
return 0;
}
m = sqrt(n);
su = m*(m+1)/2;
s = n-1;
for(i=2;i*i<=n;i++)
{
k = n/i;
s = s + (k-1)*i + k*(k+1)/2 - su;
}
cout<<s<<"\n";
}
Python
import math
n = int(input())
if n==0:
print('0')
else:
m = int(math.sqrt(n))
su = int(m*(m+1)/2)
s = n-1
i=2
while i*i<=n:
k = int(n/i)
s = s + ((k-1)*i) + int(k*(k+1)/2) - su
i = i+1
print(s)
对于 c++ 代码输出的 1000000000 的答案不同= 322467033612360628
对于 python 代码输出 = 322467033612360629
为什么答案不同?我不认为它是由 c++ 整数溢出引起的,因为在 64 位环境中 unsigned long long int 的范围是 18,446,744,073,709,551,615
编辑:删除它造成混乱的变量 t