Sum of LCM ( 1 , n ) , LCM ( 2 , n ) , LCM ( 3 , n ) , ... , LCM ( n , n ) | Python3 implementation of the approach ; Euler totient Function ; Function to return the required LCM sum ; Summation of d * ETF ( d ) where d belongs to set of divisors of n ; Driver code
n = 100002 ; NEW_LINE phi = [ 0 ] * ( n + 2 ) ; NEW_LINE ans = [ 0 ] * ( n + 2 ) ; NEW_LINE def ETF ( ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT phi [ i ] = i ; NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( phi [ i ] == i ) : NEW_LINE INDENT phi [ i ] = i - 1 ; NEW_LINE for j in range ( 2 * i , n + 1 , i ) : NEW_LINE INDENT phi [ j ] = ( phi [ j ] * ( i - 1 ) ) // i ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def LcmSum ( m ) : NEW_LINE INDENT ETF ( ) ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( i , n + 1 , i ) : NEW_LINE INDENT ans [ j ] += ( i * phi [ i ] ) ; NEW_LINE DEDENT DEDENT answer = ans [ m ] ; NEW_LINE answer = ( answer + 1 ) * m ; NEW_LINE answer = answer // 2 ; NEW_LINE return answer ; NEW_LINE DEDENT m = 5 ; NEW_LINE print ( LcmSum ( m ) ) ; NEW_LINE