# Sum of all the multiples of 3 and 7 below N

Given a number *N*, the task is to find the sum of all the multiples of *3* and *7* below *N*.

**Note:** A number must not repeat itself in the sum.

**Examples:**

Input:N = 10

Output:25

3 + 6 + 7 + 9 = 25

Input:N = 24

Output:105

3 + 6 + 7 + 9 + 12 + 14 + 15 + 18 + 21 = 105

**Approach:**

- We know that multiples of
*3*form an AP as*S*_{3}= 3 + 6 + 9 + 12 + 15 + 18 + 21 + … - And the multiples of
*7*form an AP as*S*_{7}= 7 + 14 + 21 + 28 + … - Now,
*Sum = S*i.e._{3}+ S_{7}*3 + 6 + 7 + 9 + 12 + 14 + 15 + 18 + 21 + 21 + …* - From the previous step,
*21*is repeated twice. In fact, all of the multiples of*21*(or 3*7) will be repeated as they are counted twice, once in the series*S*and again in the series_{3}*S*. So, the multiples of_{7}*21*need to be discarded from the result. - So, the final result will be
*S*_{3}+ S_{7}– S_{21}

The formula for the sum of an AP series is :

n * ( a + l ) / 2

Where n is the number of terms, a is the starting term, and l is the last term.

Below is the implementation of the above approach:

## C++

`// CPP program to find the sum of all ` `// multiples of 3 and 7 below N ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find sum of AP series ` `long` `long` `sumAP(` `long` `long` `n, ` `long` `long` `d) ` `{ ` ` ` `// Number of terms ` ` ` `n /= d; ` ` ` ` ` `return` `(n) * (1 + n) * d / 2; ` `} ` ` ` `// Function to find the sum of all ` `// multiples of 3 and 7 below N ` `long` `long` `sumMultiples(` `long` `long` `n) ` `{ ` ` ` `// Since, we need the sum of ` ` ` `// multiples less than N ` ` ` `n--; ` ` ` ` ` `return` `sumAP(n, 3) + sumAP(n, 7) - sumAP(n, 21); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `long` `long` `n = 24; ` ` ` ` ` `cout << sumMultiples(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the sum of all ` `// multiples of 3 and 7 below N ` `import` `java.util.*; ` ` ` `class` `solution ` `{ ` ` ` `// Function to find sum of AP series ` `static` `long` `sumAP(` `long` `n, ` `long` `d) ` `{ ` ` ` `// Number of terms ` ` ` `n /= d; ` ` ` ` ` `return` `(n) * (` `1` `+ n) * d / ` `2` `; ` `} ` ` ` `// Function to find the sum of all ` `// multiples of 3 and 7 below N ` `static` `long` `sumMultiples(` `long` `n) ` `{ ` ` ` `// Since, we need the sum of ` ` ` `// multiples less than N ` ` ` `n--; ` ` ` ` ` `return` `sumAP(n, ` `3` `) + sumAP(n, ` `7` `) - sumAP(n, ` `21` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `long` `n = ` `24` `; ` ` ` ` ` `System.out.println(sumMultiples(n)); ` ` ` ` ` `} ` `} ` ` ` `//This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## Python3

# Python3 program to find the sum of

# all multiples of 3 and 7 below N

# 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 3 and 7 below N

def sumMultiples(n):

# Since, we need the sum of

# multiples less than N

n -= 1;

return int(sumAP(n, 3) +

sumAP(n, 7) –

sumAP(n, 21));

# Driver code

n = 24;

print(sumMultiples(n));

# This code is contributed

# by mits

## C#

`// C# program to find the sum of all ` `// multiples of 3 and 7 below N ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find sum of AP series ` `static` `long` `sumAP(` `long` `n, ` `long` `d) ` `{ ` ` ` `// Number of terms ` ` ` `n /= d; ` ` ` ` ` `return` `(n) * (1 + n) * d / 2; ` `} ` ` ` `// Function to find the sum of all ` `// multiples of 3 and 7 below N ` `static` `long` `sumMultiples(` `long` `n) ` `{ ` ` ` `// Since, we need the sum of ` ` ` `// multiples less than N ` ` ` `n--; ` ` ` ` ` `return` `sumAP(n, 3) + sumAP(n, 7) - ` ` ` `sumAP(n, 21); ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main(String []args) ` `{ ` ` ` `long` `n = 24; ` ` ` ` ` `Console.WriteLine(sumMultiples(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Arnab Kundu ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the sum of all ` `// multiples of 3 and 7 below N ` ` ` `// 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 3 and 7 below N ` `function` `sumMultiples(` `$n` `) ` `{ ` ` ` `// Since, we need the sum of ` ` ` `// multiples less than N ` ` ` `$n` `--; ` ` ` ` ` `return` `sumAP(` `$n` `, 3) + ` ` ` `sumAP(` `$n` `, 7) - sumAP(` `$n` `, 21); ` `} ` ` ` `// Driver code ` `$n` `= 24; ` ` ` `echo` `sumMultiples(` `$n` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

105

## Recommended Posts:

- Sum of multiples of A and B less than N
- Multiples of 3 or 7
- Find the sum of all multiples of 2 and 5 below N
- Sum of the multiples of two numbers below N
- Sum of multiples of a number up to N
- Given a number n, count all multiples of 3 and/or 5 in set {1, 2, 3, ... n}
- Find Multiples of 2 or 3 or 5 less than or equal to N
- Queries for counts of multiples in an array
- Count common elements in two arrays containing multiples of N and M
- N-th multiple in sorted list of multiples of two numbers
- Sort elements of the array that occurs in between multiples of K
- Minimum sum after subtracting multiples of k from the elements of the array
- Count of common multiples of two numbers in a range
- Rearrange all elements of array which are multiples of x in increasing order

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.