Sum of i * countDigits ( i ) ^ 2 for all i in range [ L , R ] | Javascript implementation of the approach ; Function to return the required sum ; If range is valid ; Sum of AP ; Driver code
MOD = 1000000007 function rangeSum ( l , r ) { var a = 1 , b = 9 , res = 0 ; for ( var i = 1 ; i <= 10 ; i ++ ) { var L = Math . max ( l , a ) ; var R = Math . min ( r , b ) ; if ( L <= R ) { var sum = ( L + R ) * ( R - L + 1 ) / 2 ; res += ( i * i ) * ( sum % MOD ) ; res %= MOD ; } a = a * 10 ; b = b * 10 + 9 ; } return res ; } var l = 98 , r = 102 ; document . write ( rangeSum ( l , r ) ) ;