Sum of i * countDigits ( i ) ^ 2 for all i in range [ L , R ] | C # implementation of the approach ; Function to return the required sum ; If range is valid ; Sum of AP ; Driver code
using System ; class GFG { const int MOD = 1000000007 ; static int rangeSum ( int l , int r ) { int a = 1 , b = 9 , res = 0 ; for ( int i = 1 ; i <= 10 ; i ++ ) { int L = Math . Max ( l , a ) ; int R = Math . Min ( r , b ) ; if ( L <= R ) { int sum = ( L + R ) * ( R - L + 1 ) / 2 ; res += ( i * i ) * ( sum % MOD ) ; res %= MOD ; } a = a * 10 ; b = b * 10 + 9 ; } return res ; } public static void Main ( ) { int l = 98 , r = 102 ; Console . WriteLine ( rangeSum ( l , r ) ) ; } }