Given a number N, the task is to find the sum of all the multiples of A and B below N.

**Examples:**

Input:N = 11, A= 8, B= 2Output:Sum = 30 Multiples of 8 less than 11 is 8 only. Multiples of 2 less than 11 is 2, 4, 6, 8, 10 and their sum is 30. As 8 is common in both so it is counted only once.Input:N = 100, A= 5, B= 10Output:Sum = 950

**A naive approach** is to iterate through 1 to and find the multiples of A and B and add them to sum. At the end of the loop display the sum.

**Efficient approach: ** As the multiples of A will form an AP series a, 2a, 3a….

and B forms an AP series b, 2b, 3b …

On adding the sum of these two series we will get the sum of multiples of both the numbers but there might be some common multiples so remove the duplicates from the sum of these two series by subtracting the multiples of lcm(A, B). So, subtract the series of lcm(A, B) .

So the sum of multiples of A and B less than N is **Sum(A)+Sum(B)-Sum(lcm(A, B))**.

Below is the implementation of the above approach:

## C++

`// CPP program to find the sum of all ` `// multiples of A and B below N ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define ll long long int ` ` ` `// Function to find sum of AP series ` `ll sumAP(ll n, ll d) ` `{ ` ` ` `// Number of terms ` ` ` `n /= d; ` ` ` ` ` `return` `(n) * (1 + n) * d / 2; ` `} ` ` ` `// Function to find the sum of all ` `// multiples of A and B below N ` `ll sumMultiples(ll A, ll B, ll n) ` `{ ` ` ` `// Since, we need the sum of ` ` ` `// multiples less than N ` ` ` `n--; ` ` ` ` ` `// common factors of A and B ` ` ` `ll common = (A * B) / __gcd(A, B); ` ` ` ` ` `return` `sumAP(n, A) + sumAP(n, B) - sumAP(n, common); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `ll n = 100, A = 5, B = 10; ` ` ` ` ` `cout << ` `"Sum = "` `<< sumMultiples(A, B, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the sum of all ` `// multiples of A and B below N ` ` ` `class` `GFG{ ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `if` `(b == ` `0` `) ` ` ` `return` `a; ` ` ` `return` `__gcd(b, a % b); ` ` ` `} ` ` ` `// Function to find sum of AP series ` `static` `int` `sumAP(` `int` `n, ` `int` `d) ` `{ ` ` ` `// Number of terms ` ` ` `n /= d; ` ` ` ` ` `return` `(n) * (` `1` `+ n) * d / ` `2` `; ` `} ` ` ` `// Function to find the sum of all ` `// multiples of A and B below N ` `static` `int` `sumMultiples(` `int` `A, ` `int` `B, ` `int` `n) ` `{ ` ` ` `// Since, we need the sum of ` ` ` `// multiples less than N ` ` ` `n--; ` ` ` ` ` `// common factors of A and B ` ` ` `int` `common = (A * B) / __gcd(A,B); ` ` ` ` ` `return` `sumAP(n, A) + sumAP(n, B) - sumAP(n, common); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `100` `, A = ` `5` `, B = ` `10` `; ` ` ` ` ` `System.out.println(` `"Sum = "` `+sumMultiples(A, B, n)); ` `} ` `} ` `// this code is contributed by mits ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find the sum of ` `# all multiples of A and B below N ` `from` `math ` `import` `gcd,sqrt ` ` ` `# Function to find sum of AP series ` `def` `sumAP(n, d): ` ` ` ` ` `# Number of terms ` ` ` `n ` `=` `int` `(n ` `/` `d) ` ` ` ` ` `return` `(n) ` `*` `(` `1` `+` `n) ` `*` `d ` `/` `2` ` ` `# Function to find the sum of all ` `# multiples of A and B below N ` `def` `sumMultiples(A, B, n): ` ` ` ` ` `# Since, we need the sum of ` ` ` `# multiples less than N ` ` ` `n ` `-` `=` `1` ` ` ` ` `# common factors of A and B ` ` ` `common ` `=` `int` `((A ` `*` `B) ` `/` `gcd(A, B)) ` ` ` ` ` `return` `(sumAP(n, A) ` `+` `sumAP(n, B) ` `-` ` ` `sumAP(n, common)) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `100` ` ` `A ` `=` `5` ` ` `B ` `=` `10` ` ` ` ` `print` `(` `"Sum ="` `, ` `int` `(sumMultiples(A, B, n))) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the sum of all ` `// multiples of A and B below N ` ` ` `class` `GFG{ ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` `return` `__gcd(b, a % b); ` ` ` `} ` ` ` `// Function to find sum of AP series ` `static` `int` `sumAP(` `int` `n, ` `int` `d) ` `{ ` ` ` `// Number of terms ` ` ` `n /= d; ` ` ` ` ` `return` `(n) * (1 + n) * d / 2; ` `} ` ` ` `// Function to find the sum of all ` `// multiples of A and B below N ` `static` `int` `sumMultiples(` `int` `A, ` `int` `B, ` `int` `n) ` `{ ` ` ` `// Since, we need the sum of ` ` ` `// multiples less than N ` ` ` `n--; ` ` ` ` ` `// common factors of A and B ` ` ` `int` `common = (A * B) / __gcd(A,B); ` ` ` ` ` `return` `sumAP(n, A) + sumAP(n, B) - sumAP(n, common); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 100, A = 5, B = 10; ` ` ` ` ` `System.Console.WriteLine(` `"Sum = "` `+sumMultiples(A, B, n)); ` `} ` `} ` `// this code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the sum of all ` `// multiples of A and B below N ` `function` `__gcd(` `$a` `,` `$b` `) ` `{ ` ` ` `if` `(` `$b` `== 0) ` ` ` `return` `$a` `; ` ` ` `return` `__gcd(` `$b` `, ` `$a` `% ` `$b` `); ` `} ` ` ` `// Function to find sum of AP series ` `function` `sumAP(` `$n` `, ` `$d` `) ` `{ ` ` ` `// Number of terms ` ` ` `$n` `= (int)(` `$n` `/ ` `$d` `); ` ` ` ` ` `return` `(` `$n` `) * (1 + ` `$n` `) * ` `$d` `/ 2; ` `} ` ` ` `// Function to find the sum of all ` `// multiples of A and B below N ` `function` `sumMultiples(` `$A` `, ` `$B` `, ` `$n` `) ` `{ ` ` ` `// Since, we need the sum of ` ` ` `// multiples less than N ` ` ` `$n` `--; ` ` ` ` ` `// common factors of A and B ` ` ` `$common` `= (int)((` `$A` `* ` `$B` `) / ` ` ` `__gcd(` `$A` `, ` `$B` `)); ` ` ` ` ` `return` `sumAP(` `$n` `, ` `$A` `) + ` ` ` `sumAP(` `$n` `, ` `$B` `) - ` ` ` `sumAP(` `$n` `, ` `$common` `); ` `} ` ` ` `// Driver code ` `$n` `= 100; ` `$A` `= 5; ` `$B` `= 10; ` ` ` `echo` `"Sum = "` `. sumMultiples(` `$A` `, ` `$B` `, ` `$n` `); ` ` ` `// This code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Sum = 950

## Recommended Posts:

- Find Multiples of 2 or 3 or 5 less than or equal to N
- Count of Multiples of A ,B or C less than or equal to N
- Largest number less than N with digit sum greater than the digit sum of N
- Highest and Smallest power of K less than and greater than equal to N respectively
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Largest subset having with sum less than equal to sum of respective indices
- Find the sum of all multiples of 2 and 5 below N
- Sum of all the multiples of 3 and 7 below N
- Count pairs with sum as a prime number and less than n
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Sum of multiples of a number up to N
- Minimum sum after subtracting multiples of k from the elements of the array
- Sum of multiples of Array elements within a given range [L, R]
- Sum of the multiples of two numbers below N
- Sum of largest prime factor of each number less than equal to n
- Sum of elements of all partitions of number such that no element is less than K
- Sum of elements in an array whose difference with the mean of another array is less than k
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.