Sum of i * countDigits ( i ) ^ 2 for all i in range [ L , R ] | Python3 implementation of the approach ; Function to return the required sum ; If range is valid ; Sum of AP ; Driver code
MOD = 1000000007 ; NEW_LINE def rangeSum ( l , r ) : NEW_LINE INDENT a = 1 ; b = 9 ; res = 0 ; NEW_LINE for i in range ( 1 , 11 ) : NEW_LINE INDENT L = max ( l , a ) ; NEW_LINE R = min ( r , b ) ; NEW_LINE if ( L <= R ) : NEW_LINE INDENT sum = ( L + R ) * ( R - L + 1 ) // 2 ; NEW_LINE res += ( i * i ) * ( sum % MOD ) ; NEW_LINE res %= MOD ; NEW_LINE DEDENT a *= 10 ; NEW_LINE b = b * 10 + 9 ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 98 ; r = 102 ; NEW_LINE print ( rangeSum ( l , r ) ) ; NEW_LINE DEDENT