Sum of the multiples of two numbers below N
Given three integer A, B and N. The task is to find the sum of all the elements below N which are multiples of either A or B.
Examples:
Input: N = 10, A = 3, B = 5
Output: 23
3, 5, 6 and 9 are the only numbers below 10 which are multiples of either 3 or 5Input: N = 50, A = 8, B = 15
Output: 258
Naive approach:
- Initialise a variable sum = 0.
- Loop from 0 to n for each i check whether i % A = 0 or i % B = 0.
- If the above condition is satisfied, update sum = sum + i.
- Finally return the sum.
Below is the implementation of the above approach:
C++
// C++ program to find the sum of all the integers // below N which are multiples of either A or B #include <iostream> using namespace std; // Function to return the sum of all the integers // below N which are multiples of either A or B int findSum(int n, int a, int b) { int sum = 0; for (int i = 0; i < n; i++) // If i is a multiple of a or b if (i % a == 0 || i % b == 0) sum += i; return sum; } // Driver code int main() { int n = 10, a = 3, b = 5; cout << findSum(n, a, b); return 0; } |
Java
// Java program to find the sum of all the integers // below N which are multiples of either A or B import java.io.*; class GFG { // Function to return the sum of all the integers // below N which are multiples of either A or B static int findSum(int n, int a, int b) { int sum = 0; for (int i = 0; i < n; i++) // If i is a multiple of a or b if (i % a == 0 || i % b == 0) sum += i; return sum; } // Driver code public static void main (String[] args) { int n = 10, a = 3, b = 5; System.out.println( findSum(n, a, b)); } } // This code is contributed by anuj_67.. |
Python3
# Python 3 program to find the sum of # all the integers below N which are # multiples of either A or B # Function to return the sum of all # the integers below N which are # multiples of either A or B def findSum(n, a, b): sum = 0 for i in range(0, n, 1): # If i is a multiple of a or b if (i % a == 0 or i % b == 0): sum += i return sum # Driver code if __name__ == '__main__': n = 10 a = 3 b = 5 print(findSum(n, a, b)) # This code is contributed by # Surendra_Gangwar |
C#
// C# program to find the sum of all the integers // below N which are multiples of either A or B using System; class GFG { // Function to return the sum of all the integers // below N which are multiples of either A or B static int findSum(int n, int a, int b) { int sum = 0; for (int i = 0; i < n; i++) // If i is a multiple of a or b if (i % a == 0 || i % b == 0) sum += i; return sum; } // Driver code static void Main() { int n = 10, a = 3, b = 5; Console.WriteLine(findSum(n, a, b)); } // This code is contributed by Ryuga } |
PHP
<?php // PHP program to find the sum of all // the integers below N which are // multiples of either A or B // Function to return the sum of all // the integers below N which are // multiples of either A or B function findSum($n, $a, $b) { $sum = 0; for ($i = 0; $i < $n; $i++) // If i is a multiple of a or b if ($i % $a == 0 || $i % $b == 0) $sum += $i; return $sum; } // Driver code $n = 10; $a = 3; $b = 5; echo findSum($n, $a, $b); // This code is contributed by Sachin ?> |
23
Efficient approach:
- We know that multiples of A will form an AP as SA = (x) + (x + A) + (x + 2*A) + …
- And the multiples of B will form an AP as SB = (y) + (y + B) + (y + 2*B) + …
- Now, Sum = SA + SB but in this series the multiples of A * B will be repeated twice, once in SA and once in SB.
- So, these multiples need to be discarded from the result.
- Hence, the final result will be SA + SB – SA*B
Below is the implementation of the above approach:
C++
// C++ program to find the sum of all the integers // below N which are multiples of either A or B #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 a and b below n long long sumMultiples(long long n, long long a, long long b) { // Since, we need the sum of // multiples less than N n--; return sumAP(n, a) + sumAP(n, b) - sumAP(n, a * b); } // Driver code int main() { long long n = 10, a = 3, b = 5; cout << sumMultiples(n, a, b); return 0; } |
Java
// Java program to find the sum of all the integers // below N which are multiples of either A or B import java.io.*; class GFG { // Function to find sum of AP series static long sumAP(long n, long d) { // Number of terms n = (int)n/d; return (n) * (1 + n) * d / 2; } // Function to find the sum of all // multiples of a and b below n static long sumMultiples( long n, long a, long b) { // Since, we need the sum of // multiples less than N n--; return sumAP(n, a) + sumAP(n, b) - sumAP(n, a * b); } // Driver code public static void main (String[] args) { long n = 10, a = 3, b = 5; System.out.println (sumMultiples(n, a, b)); } //This code is contributed by akt_mit } |
C#
// C# program to find the sum of all the integers // below N which are multiples of either A or B using System; public class GFG{ // Function to find sum of AP series static long sumAP(long n, long d) { // Number of terms n = (int)n/d; return (n) * (1 + n) * d / 2; } // Function to find the sum of all // multiples of a and b below n static long sumMultiples( long n, long a, long b) { // Since, we need the sum of // multiples less than N n--; return sumAP(n, a) + sumAP(n, b) - sumAP(n, a * b); } // Driver code static public void Main (){ long n = 10, a = 3, b = 5; Console.WriteLine(sumMultiples(n, a, b)); } //This code is contributed by ajit. } |
PHP
<?php // PHP program to find the sum of all the integers // below N which are multiples of either A or B // Function to find sum of AP series function 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 a and b below n function sumMultiples( $n, $a, $b) { // Since, we need the sum of // multiples less than N $n--; return sumAP($n, $a) + sumAP($n, $b) - sumAP($n, $a * $b); } // Driver code { $n = 10; $a = 3; $b = 5; echo(sumMultiples($n, $a, $b)); return 0; } //This code is contributed by Mukul Singh. |
23
Recommended Posts:
- Count of common multiples of two numbers in a range
- N-th multiple in sorted list of multiples of two numbers
- Multiples of 3 or 7
- Sum of multiples of A and B less than N
- Sum of all the multiples of 3 and 7 below N
- Sum of multiples of a number up to N
- Find the sum of all multiples of 2 and 5 below N
- Given a number n, count all multiples of 3 and/or 5 in set {1, 2, 3, ... n}
- Find Multiples of 2 or 3 or 5 less than or equal to N
- Queries for counts of multiples in an array
- Sort elements of the array that occurs in between multiples of K
- Minimum sum after subtracting multiples of k from the elements of the array
- Rearrange all elements of array which are multiples of x in increasing order
- Numbers less than N which are product of exactly two distinct prime numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
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.



