Sum of all the multiples of 3 and 7 below N - GeeksforGeeks

05 Apr, 2021
Given a number N, the task is to find the sum of all the multiples of 3 and 7 below N. Note: A number must not repeat itself in the sum.
Examples:  
Input: N = 10 Output: 25 3 + 6 + 7 + 9 = 25
Input: N = 24 Output: 105 3 + 6 + 7 + 9 + 12 + 14 + 15 + 18 + 21 = 105 
Approach:  
We know that multiples of 3 form an AP as S3 = 3 + 6 + 9 + 12 + 15 + 18 + 21 + ...
And the multiples of 7 form an AP as S7 = 7 + 14 + 21 + 28 + ...
Now, Sum = S3 + S7 i.e. 3 + 6 + 7 + 9 + 12 + 14 + 15 + 18 + 21 + 21 + ...
From the previous step, 21 is repeated twice. In fact, all of the multiples of 21 (or 3*7) will be repeated as they are counted twice, once in the series S3 and again in the series S7. So, the multiples of 21 need to be discarded from the result.
So, the final result will be S3 + S7 – S21
The formula for the sum of an AP series is : n * ( a + l ) / 2 Where n is the number of terms, a is the starting term, and l is the last term.  
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find the sum of all// multiples of 3 and 7 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 3 and 7 below Nlong long sumMultiples(long long n){    // Since, we need the sum of    // multiples less than N    n--;     return sumAP(n, 3) + sumAP(n, 7) - sumAP(n, 21);} // Driver codeint main(){    long long n = 24;     cout << sumMultiples(n);     return 0;}
// Java program to find the sum of all// multiples of 3 and 7 below Nimport java.util.*; class solution{ // 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 3 and 7 below Nstatic long sumMultiples(long n){    // Since, we need the sum of    // multiples less than N    n--;     return sumAP(n, 3) + sumAP(n, 7) - sumAP(n, 21);} // Driver codepublic static void main(String args[]){    long n = 24;     System.out.println(sumMultiples(n));  }} //This code is contributed by Surendra_Gangwar
# Python3 program to find the sum of# all multiples of 3 and 7 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 3 and 7 below Ndef sumMultiples(n):     # Since, we need the sum of    # multiples less than N    n -= 1;     return int(sumAP(n, 3) +               sumAP(n, 7) -               sumAP(n, 21)); # Driver coden = 24; print(sumMultiples(n)); # This code is contributed# by mits
// C# program to find the sum of all// multiples of 3 and 7 below Nusing System; 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 3 and 7 below Nstatic long sumMultiples(long n){    // Since, we need the sum of    // multiples less than N    n--;     return sumAP(n, 3) + sumAP(n, 7) -                         sumAP(n, 21);} // Driver codestatic public void Main(String []args){    long n = 24;     Console.WriteLine(sumMultiples(n));}} // This code is contributed// by Arnab Kundu
<?php// PHP program to find the sum of all// multiples of 3 and 7 below N // Function to find sum of AP seriesfunction 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 3 and 7 below Nfunction sumMultiples($n){    // Since, we need the sum of    // multiples less than N    $n--;     return sumAP($n, 3) +           sumAP($n, 7) - sumAP($n, 21);} // Driver code$n = 24; echo sumMultiples($n); // This code is contributed// by Akanksha Rai?>
<script> // JavaScript program to find the sum of all// multiples of 3 and 7 below N // Function to find sum of AP seriesfunction sumAP(n, d){         // Number of terms    n = parseInt(n / d);     return (n) * (1 + n) * (d / 2);} // Function to find the sum of all// multiples of 3 and 7 below Nfunction sumMultiples(n){         // Since, we need the sum of    // multiples less than N    n--;     return sumAP(n, 3) +           sumAP(n, 7) -           sumAP(n, 21);} // Driver codelet n = 24; document.write(sumMultiples(n)); // This code is contributed by mohan1240760 </script>
105
 
SURENDRA_GANGWAR
andrew1234
Akanksha_Rai
Mithun Kumar
pulamolusaimohan
divisibility
Competitive Programming
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multistage Graph (Shortest Path)
Breadth First Traversal ( BFS ) on a 2D array
Shortest path in a directed graph by Dijkstra’s algorithm
5 Best Books for Competitive Programming
Check whether bitwise AND of a number with any subset of an array is zero or not
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7