sum of factors of all numbers from 1 to n

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…

1 Like

thanxx @taran_1407 you are always there to clear doubt

No problem @pandey_96