Sum of multiples of A and B less than N | CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE ll sumAP ( ll n , ll d ) { n /= d ; return ( n ) * ( 1 + n ) * d / 2 ; } ll sumMultiples ( ll A , ll B , ll n ) { n -- ; ll common = ( A * B ) / __gcd ( A , B ) ; return sumAP ( n , A ) + sumAP ( n , B ) - sumAP ( n , common ) ; } int main ( ) { ll n = 100 , A = 5 , B = 10 ; cout << " Sum ▁ = ▁ " << sumMultiples ( A , B , n ) ; return 0 ; }