# Sum of all numbers divisible by 6 in a given range

Given a range L-R, find the sum of all numbers divisible by 6 in range L-R

L and R are very large.

Examples:

Input : 1 20 Output : 36 Explanation: 6 + 12 + 18 = 36 Input : 5 7 Output : 6 Explanation: 6 is the only divisible number in range 5-7

A **naive approach **is be to run a loop from L to R and sum up all the numbers divisible by 6.

An **efficient approach **is to sum all the numbers divisible by 6 up to R in sum, and sum all numbers divisible by 6 up to L-1. And then there subtraction will be the answer.

sum = 6 + 12 + 8 + …….(R/6)terms.

sum = 6(1 + 2 + 3……R/6 terms)

sumR = 3 * (R/6) * (R/6+1)

similarly we get

sumL as 3 * ((L-1)/6) * ((L-1/6)+1)

and the final answer as sumR – sumL.

## C++

`// CPP program to find sum of numbers divisible ` `// by 6 in a given range. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to calculate the sum of ` `// all numbers divisible by 6 in range L-R.. ` `int` `sum(` `int` `L, ` `int` `R) ` `{ ` ` ` `// no of multiples of 6 upto r ` ` ` `int` `p = R / 6; ` ` ` ` ` `// no of multiples of 6 upto l-1 ` ` ` `int` `q = (L - 1) / 6; ` ` ` ` ` `// summation of all multiples of 6 upto r ` ` ` `int` `sumR = 3 * (p * (p + 1)); ` ` ` ` ` `// summation of all multiples of 6 upto l-1 ` ` ` `int` `sumL = (q * (q + 1)) * 3; ` ` ` ` ` `// returns the answer ` ` ` `return` `sumR - sumL; ` `} ` ` ` `// driver program to test the above function ` `int` `main() ` `{ ` ` ` `int` `L = 1, R = 20; ` ` ` `cout << sum(L, R); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find sum of numbers ` `// divisible by 6 in a given range. ` `import` `java.io.*; ` `class` `GFG { ` ` ` `// function to calculate the sum ` `// of all numbers divisible by 6 ` `// in range L-R.. ` `static` `int` `sum(` `int` `L, ` `int` `R) ` `{ ` ` ` `// no of multiples of 6 upto r ` ` ` `int` `p = R / ` `6` `; ` ` ` ` ` `// no of multiples of 6 upto l-1 ` ` ` `int` `q = (L - ` `1` `) / ` `6` `; ` ` ` ` ` `// summation of all multiples of ` ` ` `// 6 upto r ` ` ` `int` `sumR = ` `3` `* (p * (p + ` `1` `)); ` ` ` ` ` `// summation of all multiples of ` ` ` `// 6 upto l-1 ` ` ` `int` `sumL = (q * (q + ` `1` `)) * ` `3` `; ` ` ` ` ` `// returns the answer ` ` ` `return` `sumR - sumL; ` `} ` ` ` `// driver program ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `L = ` `1` `, R = ` `20` `; ` ` ` `System.out.println(sum(L, R)); ` `} ` `} ` ` ` `// This code is contributed by Prerna Saini ` |

*chevron_right*

*filter_none*

## Python

`# Python3 program to find sum of numbers divisible ` `# by 6 in a given range. ` ` ` `def` `sumDivisible(L, R): ` ` ` ` ` `# no of multiples of 6 upto r ` ` ` `p ` `=` `int` `(R` `/` `6` `) ` ` ` ` ` `# no of multiples of 6 upto l-1 ` ` ` `q ` `=` `int` `((L` `-` `1` `)` `/` `6` `) ` ` ` ` ` `# summation of all multiples of 6 upto r ` ` ` `sumR ` `=` `3` `*` `(p ` `*` `(p ` `+` `1` `)) ` ` ` ` ` `# summation of all multiples of 6 upto l-1 ` ` ` `sumL ` `=` `(q ` `*` `(q ` `+` `1` `)) ` `*` `3` ` ` ` ` `# returns the answer ` ` ` `return` `sumR ` `-` `sumL ` ` ` `# driver code ` `L ` `=` `1` `R ` `=` `20` `print` `(sumDivisible(L,R)) ` ` ` `# This code is contributed by 'Abhishek Sharma 44'. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum of numbers ` `// divisible by 6 in a given range. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to calculate the sum ` ` ` `// of all numbers divisible by 6 ` ` ` `// in range L-R.. ` ` ` `static` `int` `sum(` `int` `L, ` `int` `R) ` ` ` `{ ` ` ` ` ` `// no of multiples of 6 upto r ` ` ` `int` `p = R / 6; ` ` ` ` ` `// no of multiples of 6 upto l-1 ` ` ` `int` `q = (L - 1) / 6; ` ` ` ` ` `// summation of all multiples of ` ` ` `// 6 upto r ` ` ` `int` `sumR = 3 * (p * (p + 1)); ` ` ` ` ` `// summation of all multiples of ` ` ` `// 6 upto l-1 ` ` ` `int` `sumL = (q * (q + 1)) * 3; ` ` ` ` ` `// returns the answer ` ` ` `return` `sumR - sumL; ` ` ` `} ` ` ` ` ` `// driver program ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `L = 1, R = 20; ` ` ` ` ` `Console.WriteLine(sum(L, R)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find sum of numbers ` `// divisible by 6 in a given range. ` ` ` `// function to calculate the sum of ` `// all numbers divisible by 6 in range L-R.. ` `function` `sum(` `$L` `, ` `$R` `) ` `{ ` ` ` ` ` `// no of multiples of 6 upto r ` ` ` `$p` `= ` `intval` `(` `$R` `/ 6); ` ` ` ` ` `// no of multiples of 6 upto l-1 ` ` ` `$q` `= ` `intval` `((` `$L` `- 1) / 6); ` ` ` ` ` `// summation of all multiples ` ` ` `// of 6 upto r ` ` ` `$sumR` `= ` `intval` `(3 * (` `$p` `* (` `$p` `+ 1))); ` ` ` ` ` `// summation of all multiples ` ` ` `// of 6 upto l-1 ` ` ` `$sumL` `= ` `intval` `((` `$q` `* (` `$q` `+ 1)) * 3); ` ` ` ` ` `// returns the answer ` ` ` `return` `$sumR` `- ` `$sumL` `; ` `} ` ` ` `// Driver Code ` `$L` `= 1; ` `$R` `= 20; ` `echo` `sum(` `$L` `, ` `$R` `); ` ` ` `// This code is contributed by Sam007 ` `?> ` |

*chevron_right*

*filter_none*

Output:

36

Time Complexity: O(1)

## Recommended Posts:

- 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 between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Check if there is any pair in a given range with GCD is divisible by k
- Maximum positive integer divisible by C and is in the range [A, B]
- Minimum positive integer divisible by C and is not in range [A, B]
- Count integers in a range which are divisible by their euler totient value
- Sum of largest divisible powers of p (a prime number) in a range
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Count numbers which are divisible by all the numbers from 2 to 10
- Sum of numbers from 1 to N which are divisible by 3 or 4
- Sum of first N natural numbers which are divisible by 2 and 7

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.