Sum of multiples of Array elements within a given range [ L , R ] | Function to find the sum of all multiples of N up to K ; Calculate the sum ; Return the sum ; Function to find the total sum ; If L is divisible by a [ i ] ; Otherwise ; Return the final sum ; Given array arr [ ] ; Given range ; Function Call
function calcSum ( k , n ) { let value = ( k * n * ( n + 1 ) ) / 2 ; return value ; } function findSum ( a , n , L , R ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( L % a [ i ] == 0 && L != 0 ) { sum += calcSum ( a [ i ] , Math . floor ( R / a [ i ] ) ) - calcSum ( a [ i ] , Math . floor ( ( L - 1 ) / a [ i ] ) ) ; } else { sum += calcSum ( a [ i ] , Math . floor ( R / a [ i ] ) ) - calcSum ( a [ i ] , Math . floor ( L / a [ i ] ) ) ; } } return sum ; } let arr = [ 2 , 7 , 3 , 8 ] ; let N = arr . length ; let L = 7 ; let R = 20 ; document . write ( findSum ( arr , N , L , R ) ) ;