Find the sum of all multiples of 2 and 5 below N
Given a number N. The task is to find the sum of all multiples of 2 and 5 below N ( N may be up to 10^10).
Examples:
Input : N = 10 Output : 25 Explanation : 2 + 4 + 6 + 8 + 5 Input : N = 20 Output : 110
Approach :
We know that multiples of 2 form an AP as:
2, 4, 6, 8, 10, 12, 14….(1)
Similarly, multiples of 5 form an AP as:
5, 10, 15……(2)
Now, Sum(1) + Sum(2) = 2, 4, 5, 6, 8, 10, 10, 12, 14, 15….
Here, 10 is repeated. In fact, all of the multiples of 10 or 2*5 are repeated because it is counted twice, once in the series of 2 and again in the series of 5. Hence we’ll subtract the sum of the series of 10 from Sum(1) + Sum(2).
The formula for the sum of an A.P is :
n * ( a + l ) / 2
Where
is the number of terms,
is the starting term, and
is the last term.
So, the Final answer is:
S2 + S5 – S10
Below is the implementation of the above approach:
C++
// CPP program to find the sum of all // multiples of 2 and 5 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 2 and 5 below N long long sumMultiples(long long n) { // Since, we need the sum of // multiples less than N n--; return sumAP(n, 2) + sumAP(n, 5) - sumAP(n, 10); } // Driver code int main() { long long n = 20; cout << sumMultiples(n); return 0; } |
Java
// Java program to find the sum of all // multiples of 2 and 5 below N 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 2 and 5 below N static long sumMultiples(long n) { // Since, we need the sum of // multiples less than N n--; return sumAP(n, 2) + sumAP(n, 5) - sumAP(n, 10); } // Driver code public static void main(String[] args) { long n = 20; System.out.println(sumMultiples(n)); } } // This code is contributed by mits |
Python3
# Python3 program to find the sum of # all multiples of 2 and 5 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 2 and 5 below N def sumMultiples(n): # Since, we need the sum of # multiples less than N n -= 1; return (int(sumAP(n, 2) + sumAP(n, 5) - sumAP(n, 10))); # Driver code n = 20; print(sumMultiples(n)); # This code is contributed by mits |
C#
// C# program to find the sum of all // multiples of 2 and 5 below N using System; public 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 2 and 5 below N static long sumMultiples(long n) { // Since, we need the sum of // multiples less than N n--; return sumAP(n, 2) + sumAP(n, 5) - sumAP(n, 10); } // Driver code static public void Main (){ long n = 20; Console.WriteLine(sumMultiples(n)); } } |
PHP
<?php // PHP program to find the sum of all // multiples of 2 and 5 below N // Function to find sum of AP series function sumAP($n, $d) { // Number of terms $n = (int)($n /$d); return ($n) * ((1 + $n) * (int)$d / 2); } // Function to find the sum of all // multiples of 2 and 5 below N function sumMultiples($n) { // Since, we need the sum of // multiples less than N $n--; return sumAP($n, 2) + sumAP($n, 5) - sumAP($n, 10); } // Driver code $n = 20; echo sumMultiples($n); // This code is contributed // by Sach_Code ?> |
110
Recommended Posts:
- Find Multiples of 2 or 3 or 5 less than or equal to N
- Sum of multiples of A and B less than N
- Multiples of 3 or 7
- Sum of all the multiples of 3 and 7 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}
- Queries for counts of multiples in an array
- Sort elements of the array that occurs in between multiples of K
- Count common elements in two arrays containing multiples of N and M
- Count of common multiples of two numbers in a range
- N-th multiple in sorted list of multiples of two numbers
- Minimum sum after subtracting multiples of k from the elements of the array
- Print multiples of Unit Digit of Given Number
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
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.



