Given a number n. The task is to find the sum of numbers up to n, that are divisible 2 or 5.

**Examples:**

Input: n = 2 Output: 2 Input: n = 5 Output: 11

A **naive approach** is to just iterate over the numbers upto n and check if it divisible by 2 or 5. If it is divisible then just add this number to our required sum. And finally we got our total sum with complexity of O(n).

**Efficient Approach:**

- First find the numbers that are divisible by 2. So, these number for an AP, having

first term = 2, difference = 2, Number of terms = n/2

So, sum given by- - Secondly we find the numbers that are divisible by 5. So, these number for an AP, having

first term = 5, difference = 5, Number of terms = n/5

So, sum given by- - First we find the numbers that are divisible by 2 and 5.so, these number for an AP, having

first term =10, difference = 10, Number of terms = n / 10

So, sum given by- - As we have to find sum of number divisible by 2 or 5. So, required sum is given by-

sum = sum_2 + sum_5 – sum_10

Below is the implementation of above approach:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `#define ll long long int ` `using` `namespace` `std; ` ` ` `// Function to find the sum ` `ll findSum(` `int` `n) ` `{ ` ` ` ` ` `ll sum2, sum5, sum10; ` ` ` ` ` `// sum2 is sum of numbers divisible by 2 ` ` ` `sum2 = ((n / 2) * (4 + (n / 2 - 1) * 2)) / 2; ` ` ` ` ` `// sum5 is sum of number divisible by 5 ` ` ` `sum5 = ((n / 5) * (10 + (n / 5 - 1) * 5)) / 2; ` ` ` ` ` `// sum10 of numbers divisible by 2 and 5 ` ` ` `sum10 = ((n / 10) * (20 + (n / 10 - 1) * 10)) / 2; ` ` ` ` ` `return` `sum2 + sum5 - sum10; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `cout << findSum(n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of ` `// above approach ` `import` `java.lang.*; ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the sum ` `static` `long` `findSum(` `int` `n) ` `{ ` ` ` `long` `sum2, sum5, sum10; ` ` ` ` ` `// sum2 is sum of numbers ` ` ` `// divisible by 2 ` ` ` `sum2 = ((n / ` `2` `) * (` `4` `+ ` ` ` `(n / ` `2` `- ` `1` `) * ` `2` `)) / ` `2` `; ` ` ` ` ` `// sum5 is sum of number ` ` ` `// divisible by 5 ` ` ` `sum5 = ((n / ` `5` `) * (` `10` `+ ` ` ` `(n / ` `5` `- ` `1` `) * ` `5` `)) / ` `2` `; ` ` ` ` ` `// sum10 of numbers divisible ` ` ` `// by 2 and 5 ` ` ` `sum10 = ((n / ` `10` `) * (` `20` `+ ` ` ` `(n / ` `10` `- ` `1` `) * ` `10` `)) / ` `2` `; ` ` ` ` ` `return` `sum2 + sum5 - sum10; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.println(findSum(n)); ` `} ` `} ` ` ` `// This code is contributed by Raj ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of ` `# above approach ` ` ` `# Function to find the sum ` `def` `findSum(n): ` ` ` ` ` ` ` `# sum2 is sum of numbers divisible by 2 ` ` ` `sum2 ` `=` `((n ` `/` `/` `2` `) ` `*` `(` `4` `+` `(n ` `/` `/` `2` `-` `1` `) ` `*` `2` `)) ` `/` `/` `2` ` ` ` ` `# sum5 is sum of number divisible by 5 ` ` ` `sum5 ` `=` `((n ` `/` `/` `5` `) ` `*` `(` `10` `+` `(n ` `/` `/` `5` `-` `1` `) ` `*` `5` `)) ` `/` `/` `2` ` ` ` ` `# sum10 of numbers divisible by 2 and 5 ` ` ` `sum10 ` `=` `((n ` `/` `/` `10` `) ` `*` `(` `20` `+` `(n ` `/` `/` `10` `-` `1` `) ` `*` `10` `)) ` `/` `/` `2` ` ` ` ` `return` `sum2 ` `+` `sum5 ` `-` `sum10; ` ` ` ` ` `# Driver code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `5` ` ` `print` `(` `int` `(findSum(n))) ` ` ` ` ` `# this code is contributed by Shivi_Aggarwal ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of ` `// above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the sum ` `static` `long` `findSum(` `int` `n) ` `{ ` ` ` `long` `sum2, sum5, sum10; ` ` ` ` ` `// sum2 is sum of numbers ` ` ` `// divisible by 2 ` ` ` `sum2 = ((n / 2) * (4 + ` ` ` `(n / 2 - 1) * 2)) / 2; ` ` ` ` ` `// sum5 is sum of number ` ` ` `// divisible by 5 ` ` ` `sum5 = ((n / 5) * (10 + ` ` ` `(n / 5 - 1) * 5)) / 2; ` ` ` ` ` `// sum10 of numbers divisible ` ` ` `// by 2 and 5 ` ` ` `sum10 = ((n / 10) * (20 + ` ` ` `(n / 10 - 1) * 10)) / 2; ` ` ` ` ` `return` `sum2 + sum5 - sum10; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `n = 5; ` ` ` `Console.WriteLine(findSum(n)); ` `} ` `} ` ` ` `// This code is contributed by inder_verma ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of above approach ` ` ` `// Function to find the sum ` `function` `findSum(` `$n` `) ` `{ ` ` ` ` ` `// sum2 is sum of numbers ` ` ` `// divisible by 2 ` ` ` `$sum2` `= ((int)(` `$n` `/ 2) * (4 + ` ` ` `((int)(` `$n` `/ 2) - 1) * 2)) / 2; ` ` ` ` ` `// sum5 is sum of number ` ` ` `// divisible by 5 ` ` ` `$sum5` `= ((int)(` `$n` `/ 5) * (10 + ` ` ` `(` `$n` `/ 5 - 1) * 5)) / 2; ` ` ` ` ` `// sum10 of numbers divisible ` ` ` `// by 2 and 5 ` ` ` `$sum10` `= ((int)(` `$n` `/ 10) * (20 + ` ` ` `(` `$n` `/ 10 - 1) * 10)) / 2; ` ` ` ` ` `return` `$sum2` `+ ` `$sum5` `- ` `$sum10` `; ` `} ` ` ` `// Driver Code ` `$n` `= 5; ` `echo` `findSum(` `$n` `); ` ` ` `// This code is contributed by Raj ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

11

## Recommended Posts:

- Sum of Fibonacci numbers at even indexes upto N terms
- Program to print tetrahedral numbers upto Nth term
- Program to print pentatope numbers upto Nth term
- Count numbers upto N which are both perfect square and perfect cube
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Sum of numbers from 1 to N which are divisible by 3 or 4
- Sum of all numbers divisible by 6 in a given range
- Sum of first N natural numbers which are divisible by X or Y
- Divisible by 37 for large numbers
- Sum of first N natural numbers which are divisible by 2 and 7
- Sum of n digit numbers divisible by a given number
- Smallest number divisible by first n numbers
- Count the numbers divisible by 'M' in a given range
- Print k numbers where all pairs are divisible by m
- Count n digit numbers divisible by given number

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.