Sum of multiples of A and B less than N | JavaScript program to find the sum of all multiples of A and B below N ; Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of A and B below N ; Since , we need the sum of multiples less than N ; common factors of A and B ; Driver code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function sumAP ( n , d ) { n = parseInt ( n / d ) ; return ( n ) * ( 1 + n ) * d / 2 ; } function sumMultiples ( A , B , n ) { n -- ; common = parseInt ( ( A * B ) / __gcd ( A , B ) ) ; return sumAP ( n , A ) + sumAP ( n , B ) - sumAP ( n , common ) ; } let n = 100 ; let A = 5 ; let B = 10 ; document . write ( " " + sumMultiples ( A , B , n ) ) ;