text: Sum of all divisors from 1 to N | Set 3 | C # program to implement the above approach ; Function to find the sum of all divisors of all numbers from 1 to N ; Stores the sum ; Marks the last point of occurence with same count ; Calculate the sum ; Return the result ; Driver Code
code: using System ; class GFG { static readonly int m = 1000000007 ; static void solve ( long n ) { long s = 0 ; for ( int l = 1 ; l <= n ; ) { int r = ( int ) ( n / ( Math . Floor ( ( double ) n / l ) ) ) ; int x = ( ( ( r % m ) * ( ( r + 1 ) % m ) ) / 2 ) % m ; int y = ( ( ( l % m ) * ( ( l - 1 ) % m ) ) / 2 ) % m ; int p = ( int ) ( ( n / l ) % m ) ; s = ( s + ( ( ( x - y ) % m ) * p ) % m + m ) % m ; s %= m ; l = r + 1 ; } Console . Write ( ( s + m ) % m ) ; } public static void Main ( String [ ] args ) { long n = 12 ; solve ( n ) ; } }