Can anyone explain me how this soln works
ll N,i,sum=0;
cin>>N;
for(i=1;i<=N;i++)
{
sum+=i*(N/i);
}
cout<<sum<<endl;
Can anyone explain me how this soln works
ll N,i,sum=0;
cin>>N;
for(i=1;i<=N;i++)
{
sum+=i*(N/i);
}
cout<<sum<<endl;
In this solution, floor(N/i) tells the number of times a multiple of i occur.
For example, for N = 7 and i = 2, N/i = 3, which is right telling us that multiple of 2 appear 3 times (2, 4, 6).
We need sum, so we multiply floor(N/i) with i to get sum.
Enjoy coding…