Find the sum of all multiples of 2 and 5 below N - GeeksforGeeks

09 Apr, 2021
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 ) / 2Where 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++
Java
Python3
C#
PHP
Javascript
// 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 serieslong 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 Nlong 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 codeint main(){    long long n = 20;     cout << sumMultiples(n);     return 0;}
// Java program to find the sum of all// multiples of 2 and 5 below N class GFG{// Function to find sum of AP seriesstatic 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 Nstatic 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 codepublic static void main(String[] args){    long n = 20;     System.out.println(sumMultiples(n));}}// This code is contributed by mits
# Python3 program to find the sum of# all multiples of 2 and 5 below N # Function to find sum of AP seriesdef 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 Ndef 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 coden = 20; print(sumMultiples(n));     # This code is contributed by mits
// 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 seriesstatic 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 Nstatic 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 program to find the sum of all// multiples of 2 and 5 below N// Function to find sum of AP seriesfunction 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 Nfunction 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?>
<script> // Java script program to find the sum of all// multiples of 2 and 5 below N // Function to find sum of AP seriesfunction sumAP(n, d){         // Number of terms    n = parseInt(n / d);     return (n) * ((1 + n) *         parseInt(d) / 2);} // Function to find the sum of all// multiples of 2 and 5 below Nfunction sumMultiples(n){         // Since, we need the sum of    // multiples less than N    n--;     return sumAP(n, 2) + sumAP(n, 5) -           sumAP(n, 10);} // Driver coden = 20; document.write( sumMultiples(n)); // This code is contributed by bobby </script>
110
 
Mithun Kumar
Sach_Code
gottumukkalabobby
arithmetic progression
factor
series
series-sum
Competitive Programming
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Breadth First Traversal ( BFS ) on a 2D array
Prefix Sum Array - Implementation and Applications in Competitive Programming
Modulo 10^9+7 (1000000007)
5 Best Languages for Competitive Programming
What is Competitive Programming and How to Prepare for It?
Program for Fibonacci numbers
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
C++ Data Types
Coin Change | DP-7