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 ; input n and m
function sumOfLastDig ( n , m ) { let sum = 0 , k ; k = parseInt ( n / m ) ; let arr = new Array ( 10 ) ; for ( let i = 0 ; i < 10 ; i ++ ) { arr [ i ] = m * ( i + 1 ) % 10 ; sum += arr [ i ] ; } let rem = k % 10 ; let ans = parseInt ( k / 10 ) * sum ; for ( let i = 0 ; i < rem ; i ++ ) { ans += arr [ i ] ; } return ans ; } let n = 100 , m = 3 ; document . write ( sumOfLastDig ( n , m ) ) ;