# Sum of all numbers in the given range which are divisible by M

Given three numbers **A, B** and **M** such that **A < B**, the task is to find the sum of numbers **divisible by M** in the range **[A, B]**.

Examples:

Input:A = 25, B = 100, M = 30

Output:180

Explanation:

In the given range [25, 100] 30, 60 and 90 are the numbers which are divisible by M = 30

Therefore, sum of these numbers = 180.

Input:A = 6, B = 15, M = 3

Output:42

Explanation:

In the given range [6, 15] 6, 9, 12 and 15 are the numbers which are divisible by M = 3.

Therefore, sum of these numbers = 42.

* Naive Approach:* Check for each number in the range [A, B] if they are divisible by M or not. And finally, add all the numbers that are divisible by M.

Below is the implementation of the above approach:

## C++

`// C++ program to find the sum of numbers ` `// divisible by M in the given range ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the sum of numbers ` `// divisible by M in the given range ` `int` `sumDivisibles(` `int` `A, ` `int` `B, ` `int` `M) ` `{ ` ` ` `// Variable to store the sum ` ` ` `int` `sum = 0; ` ` ` ` ` `// Running a loop from A to B and check ` ` ` `// if a number is divisible by i. ` ` ` `for` `(` `int` `i = A; i <= B; i++) ` ` ` ` ` `// If the number is divisible, ` ` ` `// then add it to sum ` ` ` `if` `(i % M == 0) ` ` ` `sum += i; ` ` ` ` ` `// Return the sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// A and B define the range ` ` ` `// M is the dividend ` ` ` `int` `A = 6, B = 15, M = 3; ` ` ` ` ` `// Printing the result ` ` ` `cout << sumDivisibles(A, B, M) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the sum of numbers ` `// divisible by M in the given range ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find the sum of numbers ` `// divisible by M in the given range ` `static` `int` `sumDivisibles(` `int` `A, ` `int` `B, ` `int` `M) ` `{ ` ` ` `// Variable to store the sum ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// Running a loop from A to B and check ` ` ` `// if a number is divisible by i. ` ` ` `for` `(` `int` `i = A; i <= B; i++) ` ` ` ` ` `// If the number is divisible, ` ` ` `// then add it to sum ` ` ` `if` `(i % M == ` `0` `) ` ` ` `sum += i; ` ` ` ` ` `// Return the sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `// A and B define the range ` ` ` `// M is the dividend ` ` ` `int` `A = ` `6` `, B = ` `15` `, M = ` `3` `; ` ` ` ` ` `// Printing the result ` ` ` `System.out.print(sumDivisibles(A, B, M) +` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find the sum of numbers ` `# divisible by M in the given range ` ` ` `# Function to find the sum of numbers ` `# divisible by M in the given range ` `def` `sumDivisibles(A, B, M): ` ` ` ` ` `# Variable to store the sum ` ` ` `sum` `=` `0` ` ` ` ` `# Running a loop from A to B and check ` ` ` `# if a number is divisible by i. ` ` ` `for` `i ` `in` `range` `(A, B ` `+` `1` `): ` ` ` ` ` `# If the number is divisible, ` ` ` `# then add it to sum ` ` ` `if` `(i ` `%` `M ` `=` `=` `0` `): ` ` ` `sum` `+` `=` `i ` ` ` ` ` `# Return the sum ` ` ` `return` `sum` ` ` `# Driver code ` `if` `__name__` `=` `=` `"__main__"` `: ` ` ` ` ` `# A and B define the range ` ` ` `# M is the dividend ` ` ` `A ` `=` `6` ` ` `B ` `=` `15` ` ` `M ` `=` `3` ` ` ` ` `# Printing the result ` ` ` `print` `(sumDivisibles(A, B, M)) ` ` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the sum of numbers ` `// divisible by M in the given range ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the sum of numbers ` `// divisible by M in the given range ` `static` `int` `sumDivisibles(` `int` `A, ` `int` `B, ` `int` `M) ` `{ ` ` ` `// Variable to store the sum ` ` ` `int` `sum = 0; ` ` ` ` ` `// Running a loop from A to B and check ` ` ` `// if a number is divisible by i. ` ` ` `for` `(` `int` `i = A; i <= B; i++) ` ` ` ` ` `// If the number is divisible, ` ` ` `// then add it to sum ` ` ` `if` `(i % M == 0) ` ` ` `sum += i; ` ` ` ` ` `// Return the sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `// A and B define the range ` ` ` `// M is the dividend ` ` ` `int` `A = 6, B = 15, M = 3; ` ` ` ` ` `// Printing the result ` ` ` `Console.Write(sumDivisibles(A, B, M) +` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

**Output:**

42

**Time Complexity:** O(N).

* Efficient Approach:* The idea is to use the concept of Arithmetic Progression and divisibility.

- Upon visualization, the multiples of M can be seen to form a series
M, 2M, 3M, ...

- If we can find the value of K which is the first term in the range [A, B] which is divisible by M, then directly, the series would be:
K, (K + M), (K + 2M), ------ (K + (N - 1)*M ) where N is the number of elements in the series.

- Therefore, the first term
**‘K’**in the series is nothing but the largest number smaller than or equal to A that is divisible by M. - Similarly, the last term is the smallest number greater than or equal B that is divisible by M.
- However, if any of the above numbers exceed out of the range, then we can directly subtract M from it to bring it into the range.
- And, the number of terms divisible by M can be found out by the formula:
N = B / M - (A - 1)/ M

- Therefore, the sum of the elements can be found out by:
sum = N * ( (first term + last term) / 2)

Below is the implementation of the above approach:

## C++

`// C++ program to find the sum of numbers ` `// divisible by M in the given range ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the largest number ` `// smaller than or equal to N ` `// that is divisible by K ` `int` `findSmallNum(` `int` `N, ` `int` `K) ` `{ ` ` ` `// Finding the remainder when N is ` ` ` `// divided by K ` ` ` `int` `rem = N % K; ` ` ` ` ` `// If the remainder is 0, then the ` ` ` `// number itself is divisible by K ` ` ` `if` `(rem == 0) ` ` ` `return` `N; ` ` ` `else` ` ` ` ` `// Else, then the difference between ` ` ` `// N and remainder is the largest number ` ` ` `// which is divisible by K ` ` ` `return` `N - rem; ` `} ` ` ` `// Function to find the smallest number ` `// greater than or equal to N ` `// that is divisible by K ` `int` `findLargeNum(` `int` `N, ` `int` `K) ` `{ ` ` ` `// Finding the remainder when N is ` ` ` `// divided by K ` ` ` `int` `rem = (N + K) % K; ` ` ` ` ` `// If the remainder is 0, then the ` ` ` `// number itself is divisible by K ` ` ` `if` `(rem == 0) ` ` ` `return` `N; ` ` ` `else` ` ` ` ` `// Else, then the difference between ` ` ` `// N and remainder is the largest number ` ` ` `// which is divisible by K ` ` ` `return` `N + K - rem; ` `} ` ` ` `// Function to find the sum of numbers ` `// divisible by M in the given range ` `int` `sumDivisibles(` `int` `A, ` `int` `B, ` `int` `M) ` `{ ` ` ` `// Variable to store the sum ` ` ` `int` `sum = 0; ` ` ` `int` `first = findSmallNum(A, M); ` ` ` `int` `last = findLargeNum(B, M); ` ` ` ` ` `// To bring the smallest and largest ` ` ` `// numbers in the range [A, B] ` ` ` `if` `(first < A) ` ` ` `first += M; ` ` ` ` ` `if` `(last > B) ` ` ` `first -= M; ` ` ` ` ` `// To count the number of terms in the AP ` ` ` `int` `n = (B / M) - (A - 1) / M; ` ` ` ` ` `// Sum of n terms of an AP ` ` ` `return` `n * (first + last) / 2; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// A and B define the range, ` ` ` `// M is the dividend ` ` ` `int` `A = 6, B = 15, M = 3; ` ` ` ` ` `// Printing the result ` ` ` `cout << sumDivisibles(A, B, M); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the sum of numbers ` `// divisible by M in the given range ` ` ` ` ` `class` `GFG{ ` ` ` `// Function to find the largest number ` `// smaller than or equal to N ` `// that is divisible by K ` `static` `int` `findSmallNum(` `int` `N, ` `int` `K) ` `{ ` ` ` `// Finding the remainder when N is ` ` ` `// divided by K ` ` ` `int` `rem = N % K; ` ` ` ` ` `// If the remainder is 0, then the ` ` ` `// number itself is divisible by K ` ` ` `if` `(rem == ` `0` `) ` ` ` `return` `N; ` ` ` `else` ` ` ` ` `// Else, then the difference between ` ` ` `// N and remainder is the largest number ` ` ` `// which is divisible by K ` ` ` `return` `N - rem; ` `} ` ` ` `// Function to find the smallest number ` `// greater than or equal to N ` `// that is divisible by K ` `static` `int` `findLargeNum(` `int` `N, ` `int` `K) ` `{ ` ` ` `// Finding the remainder when N is ` ` ` `// divided by K ` ` ` `int` `rem = (N + K) % K; ` ` ` ` ` `// If the remainder is 0, then the ` ` ` `// number itself is divisible by K ` ` ` `if` `(rem == ` `0` `) ` ` ` `return` `N; ` ` ` `else` ` ` ` ` `// Else, then the difference between ` ` ` `// N and remainder is the largest number ` ` ` `// which is divisible by K ` ` ` `return` `N + K - rem; ` `} ` ` ` `// Function to find the sum of numbers ` `// divisible by M in the given range ` `static` `int` `sumDivisibles(` `int` `A, ` `int` `B, ` `int` `M) ` `{ ` ` ` `// Variable to store the sum ` ` ` `int` `first = findSmallNum(A, M); ` ` ` `int` `last = findLargeNum(B, M); ` ` ` ` ` `// To bring the smallest and largest ` ` ` `// numbers in the range [A, B] ` ` ` `if` `(first < A) ` ` ` `first += M; ` ` ` ` ` `if` `(last > B) ` ` ` `first -= M; ` ` ` ` ` `// To count the number of terms in the AP ` ` ` `int` `n = (B / M) - (A - ` `1` `) / M; ` ` ` ` ` `// Sum of n terms of an AP ` ` ` `return` `n * (first + last) / ` `2` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `// A and B define the range, ` ` ` `// M is the dividend ` ` ` `int` `A = ` `6` `, B = ` `15` `, M = ` `3` `; ` ` ` ` ` `// Printing the result ` ` ` `System.out.print(sumDivisibles(A, B, M)); ` ` ` `} ` `} ` ` ` `// This code contributed by Princi Singh ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find the sum of numbers ` `# divisible by M in the given range ` ` ` `# Function to find the largest number ` `# smaller than or equal to N ` `# that is divisible by K ` `def` `findSmallNum(N, K): ` ` ` ` ` `# Finding the remainder when N is ` ` ` `# divided by K ` ` ` `rem ` `=` `N ` `%` `K ` ` ` ` ` `# If the remainder is 0, then the ` ` ` `# number itself is divisible by K ` ` ` `if` `(rem ` `=` `=` `0` `): ` ` ` `return` `N ` ` ` `else` `: ` ` ` `# Else, then the difference between ` ` ` `# N and remainder is the largest number ` ` ` `# which is divisible by K ` ` ` `return` `N ` `-` `rem ` ` ` `# Function to find the smallest number ` `# greater than or equal to N ` `# that is divisible by K ` `def` `findLargeNum(N, K): ` ` ` ` ` `# Finding the remainder when N is ` ` ` `# divided by K ` ` ` `rem ` `=` `(N ` `+` `K) ` `%` `K ` ` ` ` ` `# If the remainder is 0, then the ` ` ` `# number itself is divisible by K ` ` ` `if` `(rem ` `=` `=` `0` `): ` ` ` `return` `N ` ` ` `else` `: ` ` ` `# Else, then the difference between ` ` ` `# N and remainder is the largest number ` ` ` `# which is divisible by K ` ` ` `return` `N ` `+` `K ` `-` `rem ` ` ` `# Function to find the sum of numbers ` `# divisible by M in the given range ` `def` `sumDivisibles(A, B, M): ` ` ` ` ` `# Variable to store the sum ` ` ` `sum` `=` `0` ` ` `first ` `=` `findSmallNum(A, M) ` ` ` `last ` `=` `findLargeNum(B, M) ` ` ` ` ` `# To bring the smallest and largest ` ` ` `# numbers in the range [A, B] ` ` ` `if` `(first < A): ` ` ` `first ` `+` `=` `M ` ` ` ` ` `if` `(last > B): ` ` ` `first ` `-` `=` `M ` ` ` ` ` `# To count the number of terms in the AP ` ` ` `n ` `=` `(B ` `/` `/` `M) ` `-` `(A ` `-` `1` `) ` `/` `/` `M ` ` ` ` ` `# Sum of n terms of an AP ` ` ` `return` `n ` `*` `(first ` `+` `last) ` `/` `/` `2` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# A and B define the range, ` ` ` `# M is the dividend ` ` ` `A ` `=` `6` ` ` `B ` `=` `15` ` ` `M ` `=` `3` ` ` ` ` `# Printing the result ` ` ` `print` `(sumDivisibles(A, B, M)) ` ` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the sum of numbers ` `// divisible by M in the given range ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG{ ` ` ` `// Function to find the largest number ` `// smaller than or equal to N ` `// that is divisible by K ` `static` `int` `findSmallNum(` `int` `N, ` `int` `K) ` `{ ` ` ` `// Finding the remainder when N is ` ` ` `// divided by K ` ` ` `int` `rem = N % K; ` ` ` ` ` `// If the remainder is 0, then the ` ` ` `// number itself is divisible by K ` ` ` `if` `(rem == 0) ` ` ` `return` `N; ` ` ` `else` ` ` ` ` `// Else, then the difference between ` ` ` `// N and remainder is the largest number ` ` ` `// which is divisible by K ` ` ` `return` `N - rem; ` `} ` ` ` `// Function to find the smallest number ` `// greater than or equal to N ` `// that is divisible by K ` `static` `int` `findLargeNum(` `int` `N, ` `int` `K) ` `{ ` ` ` `// Finding the remainder when N is ` ` ` `// divided by K ` ` ` `int` `rem = (N + K) % K; ` ` ` ` ` `// If the remainder is 0, then the ` ` ` `// number itself is divisible by K ` ` ` `if` `(rem == 0) ` ` ` `return` `N; ` ` ` `else` ` ` ` ` `// Else, then the difference between ` ` ` `// N and remainder is the largest number ` ` ` `// which is divisible by K ` ` ` `return` `N + K - rem; ` `} ` ` ` `// Function to find the sum of numbers ` `// divisible by M in the given range ` `static` `int` `sumDivisibles(` `int` `A, ` `int` `B, ` `int` `M) ` `{ ` ` ` `// Variable to store the sum ` ` ` `int` `first = findSmallNum(A, M); ` ` ` `int` `last = findLargeNum(B, M); ` ` ` ` ` `// To bring the smallest and largest ` ` ` `// numbers in the range [A, B] ` ` ` `if` `(first < A) ` ` ` `first += M; ` ` ` ` ` `if` `(last > B) ` ` ` `first -= M; ` ` ` ` ` `// To count the number of terms in the AP ` ` ` `int` `n = (B / M) - (A - 1) / M; ` ` ` ` ` `// Sum of n terms of an AP ` ` ` `return` `n * (first + last) / 2; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `// A and B define the range, ` ` ` `// M is the dividend ` ` ` `int` `A = 6, B = 15, M = 3; ` ` ` ` ` `// Printing the result ` ` ` `Console.Write(sumDivisibles(A, B, M)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

42

**Time Complexity:** O(1).

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Sum of all numbers divisible by 6 in a given range
- Count the numbers divisible by 'M' in a given range
- Count numbers in range 1 to N which are divisible by X but not by Y
- Numbers that are not divisible by any number in the range [2, 10]
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count of numbers in range which are divisible by M and have digit D at odd places
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Count integers in the range [A, B] that are not divisible by C and D
- Check if there is any pair in a given range with GCD is divisible by k
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- Minimum positive integer divisible by C and is not in range [A, B]

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.