Sum of last digit of all integers from 1 to N divisible by M | Function to return the required sum ; Number of element between 1 to n divisible by m ; Array to store the last digit of elements in a cycle ; Storing and adding last digit of cycle ; Number of elements present in last cycle ; Sum of k / 10 cycle ; Adding value of digits of last cycle to the answer ; Driver Code ; input n and m
def sumOfLastDig ( n , m ) : NEW_LINE INDENT sum = 0 ; NEW_LINE k = n // m ; NEW_LINE arr = [ 0 ] * 10 ; NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT arr [ i ] = m * ( i + 1 ) % 10 ; NEW_LINE sum += arr [ i ] ; NEW_LINE DEDENT rem = k % 10 ; NEW_LINE ans = ( k // 10 ) * sum ; NEW_LINE for i in range ( rem ) : NEW_LINE INDENT ans += arr [ i ] ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 100 ; m = 3 ; NEW_LINE print ( sumOfLastDig ( n , m ) ) ; NEW_LINE DEDENT