Open In App

Sum of multiples of A and B less than N

Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to find the sum of all the multiples of A and B below N.

Examples: 

Input:N = 11, A= 8, B= 2
Output: Sum = 30
Multiples of 8 less than 11 is 8 only.
Multiples of 2 less than 11 is 2, 4, 6, 8, 10 and their sum is 30.
As 8 is common in both so it is counted only once.
Input: N = 100, A= 5, B= 10
Output: Sum = 950

Brute Force Approach:

A brute-force approach to solve this problem would be to iterate over all the numbers below N and check if each number is a multiple of A or B. If it is a multiple of either A or B, add it to the sum.

Below is the implementation of the above approach: 

C++




#include <bits/stdc++.h>
using namespace std;
#define ll long long int
 
// Function to find the sum of all
// multiples of A and B below N
ll sumMultiples(ll A, ll B, ll n)
{
    ll sum = 0;
    for(ll i=1;i<n;i++){
        if(i%A==0 || i%B==0)
            sum+=i;
    }
    return sum;
}
 
// Driver code
int main()
{
    ll n = 100, A = 5, B = 10;
    cout << "Sum = " << sumMultiples(A, B, n);
    return 0;
}


Java




import java.util.*;
 
class GFG {
 
    // Function to find the sum of all
    // multiples of A and B below N
    static long sumMultiples(long A, long B, long n)
    {
        long sum = 0;
        for (long i = 1; i < n; i++) {
            if (i % A == 0 || i % B == 0) {
                sum += i;
            }
        }
        return sum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        long n = 100, A = 5, B = 10;
        System.out.println("Sum = "
                           + sumMultiples(A, B, n));
    }
}


Python3




# Function to find the sum of all
# multiples of A and B below N
def sumMultiples(A, B, n):
    sum = 0
    for i in range(1, n):
        if i % A == 0 or i % B == 0:
            sum += i
    return sum
 
# Driver code
n, A, B = 100, 5, 10
print("Sum =", sumMultiples(A, B, n))


C#




using System;
 
class Program {
    // Function to find the sum of all
    // multiples of A and B below N
    static long sumMultiples(long A, long B, long N)
    {
        long sum = 0;
        for (long i = 1; i < N; i++) {
            if (i % A == 0 || i % B == 0) {
                sum += i;
            }
        }
        return sum;
    }
    // Driver code
    static void Main(string[] args)
    {
        long n = 100, A = 5, B = 10;
        Console.WriteLine("Sum = " + sumMultiples(A, B, n));
    }
}


Javascript




// Function to find the sum of all multiples of A and B below N
function sumMultiples(A, B, n) {
    let sum = 0;
    for (let i = 1; i < n; i++) {
        if (i % A == 0 || i % B == 0)
            sum += i;
    }
    return sum;
}
 
let n = 100,
    A = 5,
    B = 10;
console.log("Sum = " + sumMultiples(A, B, n));


Output

Sum = 950







Time Complexity: O(n)
Auxiliary Space: O(1)

Efficient approach: As the multiples of A will form an AP series a, 2a, 3a…. 
and B forms an AP series b, 2b, 3b … 
On adding the sum of these two series we will get the sum of multiples of both the numbers but there might be some common multiples so remove the duplicates from the sum of these two series by subtracting the multiples of lcm(A, B). So, subtract the series of lcm(A, B) . 
So the sum of multiples of A and B less than N is Sum(A)+Sum(B)-Sum(lcm(A, B)).

Below is the implementation of the above approach: 

C++




// CPP program to find the sum of all
// multiples of A and B below N
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
 
// Function to find sum of AP series
ll sumAP(ll n, ll 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
ll sumMultiples(ll A, ll B, ll n)
{
    // Since, we need the sum of
    // multiples less than N
    n--;
 
    // common factors of A and B
    ll common = (A * B) / __gcd(A, B);
 
    return sumAP(n, A) + sumAP(n, B) - sumAP(n, common);
}
 
// Driver code
int main()
{
    ll n = 100, A = 5, B = 10;
 
    cout << "Sum = " << sumMultiples(A, B, n);
 
    return 0;
}


Java




// Java program to find the sum of all
// multiples of A and B below N
 
class GFG{
 
static int __gcd(int a, int b)
    {
      if (b == 0)
        return a;
      return __gcd(b, a % b); 
    }
     
// Function to find sum of AP series
static int sumAP(int n, int 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
static int sumMultiples(int A, int B, int n)
{
    // Since, we need the sum of
    // multiples less than N
    n--;
 
    // common factors of A and B
    int common = (A * B) / __gcd(A,B);
 
    return sumAP(n, A) + sumAP(n, B) - sumAP(n, common);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 100, A = 5, B = 10;
 
    System.out.println("Sum = "+sumMultiples(A, B, n));
}
}
// this code is contributed by mits


Python3




# Python 3 program to find the sum of
# all multiples of A and B below N
from math import gcd,sqrt
 
# 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 A and B below N
def sumMultiples(A, B, n):
     
    # Since, we need the sum of
    # multiples less than N
    n -= 1
 
    # common factors of A and B
    common = int((A * B) / gcd(A, B))
 
    return (sumAP(n, A) + sumAP(n, B) -
            sumAP(n, common))
 
# Driver code
if __name__ == '__main__':
    n = 100
    A = 5
    B = 10
 
    print("Sum =", int(sumMultiples(A, B, n)))
 
# This code is contributed by
# Surendra_Gangwar


C#




// C# program to find the sum of all
// multiples of A and B below N
 
class GFG{
 
static int __gcd(int a, int b)
    {
    if (b == 0)
        return a;
    return __gcd(b, a % b);
    }
     
// Function to find sum of AP series
static int sumAP(int n, int 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
static int sumMultiples(int A, int B, int n)
{
    // Since, we need the sum of
    // multiples less than N
    n--;
 
    // common factors of A and B
    int common = (A * B) / __gcd(A,B);
 
    return sumAP(n, A) + sumAP(n, B) - sumAP(n, common);
}
 
// Driver code
public static void Main()
{
    int n = 100, A = 5, B = 10;
 
    System.Console.WriteLine("Sum = "+sumMultiples(A, B, n));
}
}
// this code is contributed by mits


Javascript




<script>
 
// JavaScript  program to find the sum of all
// multiples of A and B below N
function __gcd(a,b)
{
    if (b == 0)
        return a;
    return __gcd(b, a % b);
}
 
// Function to find sum of AP series
function 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 A and B below N
function sumMultiples(A, B, n)
{
    // Since, we need the sum of
    // multiples less than N
    n--;
 
    // common factors of A and B
    common = parseInt((A * B) /
               __gcd(A, B));
 
    return sumAP(n, A) +
           sumAP(n, B) -
           sumAP(n, common);
}
 
// Driver code
let n = 100;
let A = 5;
let B = 10;
 
document.write( "Sum = " + sumMultiples(A, B, n));
 
 
// This code is contributed by bobby
 
</script>


PHP




<?php
// PHP program to find the sum of all
// multiples of A and B below N
function __gcd($a,$b)
{
    if ($b == 0)
        return $a;
    return __gcd($b, $a % $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($A, $B, $n)
{
    // Since, we need the sum of
    // multiples less than N
    $n--;
 
    // common factors of A and B
    $common = (int)(($A * $B) /
               __gcd($A, $B));
 
    return sumAP($n, $A) +
           sumAP($n, $B) -
           sumAP($n, $common);
}
 
// Driver code
$n = 100;
$A = 5;
$B = 10;
 
echo "Sum = " . sumMultiples($A, $B, $n);
 
// This code is contributed by mits
?>


Output

Sum = 950







Time Complexity: O(log(min(a, b))), where a and b are two parameters of gcd.
Auxiliary Space: O(log(min(a, b)))

Approach:

  • The Euclidean algorithm can be used to find the greatest common divisor (GCD) of A and B, which is the largest positive integer that divides both A and B.
  •  Once we have the GCD, we can use it to compute the least common multiple (LCM) of A and B, which is the smallest positive integer that is divisible by both A and B.
  • This approach uses the gcd function to compute the GCD of A and B, and then computes the LCM using the formula lcmAB = A * B / gcdAB.
  •  It then computes the number of multiples of A, B, and the LCM below N, and uses the arithmetic series formula to compute the sum of each sequence. Finally, it subtracts the sum of the multiples of the LCM to avoid double-counting

Below is the implementation of the above approach: 

C++




#include <iostream>
using namespace std;
 
int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}
 
int main() {
    int N = 100;
    int A = 5;
    int B = 10;
 
    int gcdAB = gcd(A, B);
    int lcmAB = A * B / gcdAB;
 
    int numMultiplesA = (N-1) / A;
    int numMultiplesB = (N-1) / B;
    int numMultiplesLCM = (N-1) / lcmAB;
 
    int sumMultiplesA = A * (numMultiplesA * (numMultiplesA + 1)) / 2;
    int sumMultiplesB = B * (numMultiplesB * (numMultiplesB + 1)) / 2;
    int sumMultiplesLCM = lcmAB * (numMultiplesLCM * (numMultiplesLCM + 1)) / 2;
 
    int sum = sumMultiplesA + sumMultiplesB - sumMultiplesLCM;
 
    cout <<"sum = " << sum << endl;
 
    return 0;
}


Java




public class GFG {
    public static int gcd(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
 
    public static void main(String[] args) {
        int N = 100;
        int A = 5;
        int B = 10;
 
        int gcdAB = gcd(A, B);
        int lcmAB = A * B / gcdAB;
 
        int numMultiplesA = (N - 1) / A;
        int numMultiplesB = (N - 1) / B;
        int numMultiplesLCM = (N - 1) / lcmAB;
 
        int sumMultiplesA = A * (numMultiplesA * (numMultiplesA + 1)) / 2;
        int sumMultiplesB = B * (numMultiplesB * (numMultiplesB + 1)) / 2;
        int sumMultiplesLCM = lcmAB * (numMultiplesLCM * (numMultiplesLCM + 1)) / 2;
 
        int sum = sumMultiplesA + sumMultiplesB - sumMultiplesLCM;
 
        System.out.println("sum = " + sum);
    }
}


Python3




# Function to find the Greatest Common Divisor (GCD) using the Euclidean algorithm
def gcd(a, b):
    while b != 0:
        temp = b
        b = a % b
        a = temp
    return a
 
# Given values
N = 100
A = 5
B = 10
 
# Calculate the GCD of A and B
gcdAB = gcd(A, B)
 
# Calculate the Least Common Multiple (LCM) of A and B using the GCD
lcmAB = A * B // gcdAB
 
# Calculate the number of multiples of A, B, and LCM less than N
numMultiplesA = (N - 1) // A
numMultiplesB = (N - 1) // B
numMultiplesLCM = (N - 1) // lcmAB
 
# Calculate the sum of multiples of A, B, and LCM using the arithmetic series formula
sumMultiplesA = A * (numMultiplesA * (numMultiplesA + 1)) // 2
sumMultiplesB = B * (numMultiplesB * (numMultiplesB + 1)) // 2
sumMultiplesLCM = lcmAB * (numMultiplesLCM * (numMultiplesLCM + 1)) // 2
 
# Calculate the final sum by subtracting the sum of multiples of LCM from the sum of multiples of A and B
sum = sumMultiplesA + sumMultiplesB - sumMultiplesLCM
 
# Print the result
print("sum =", sum)


C#




using System;
 
class GFG
{
    // Function to calculate the Greatest Common Divisor (GCD)
   // of two numbers.
    static int GCD(int a, int b)
    {
        while (b != 0)
        {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
 
    static void Main()
    {
        int N = 100; // The limit N
        int A = 5;   // First number A
        int B = 10;  // Second number B
 
        // Calculate the GCD and LCM of A and B
        int gcdAB = GCD(A, B);
        int lcmAB = A * B / gcdAB;
 
        // Calculate the number of multiples of A, B, and LCM within the limit N.
        int numMultiplesA = (N - 1) / A;
        int numMultiplesB = (N - 1) / B;
        int numMultiplesLCM = (N - 1) / lcmAB;
 
        // Calculate the sum of all multiples of A, B, and LCM within the limit N using the sum formula for arithmetic series.
        int sumMultiplesA = A * (numMultiplesA * (numMultiplesA + 1)) / 2;
        int sumMultiplesB = B * (numMultiplesB * (numMultiplesB + 1)) / 2;
        int sumMultiplesLCM = lcmAB * (numMultiplesLCM * (numMultiplesLCM + 1)) / 2;
 
        // Calculate the final sum by subtracting the sum of LCM multiples
       // from the sum of A and B multiples.
        int sum = sumMultiplesA + sumMultiplesB - sumMultiplesLCM;
 
        // Print the result
        Console.WriteLine("sum = " + sum);
    }
}


Javascript




// Function to find the Greatest Common Divisor (GCD) using the Euclidean algorithm
function gcd(a, b) {
    while (b !== 0) {
        const temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}
 
    const N = 100;
    const A = 5;
    const B = 10;
 
    const gcdAB = gcd(A, B);
    const lcmAB = (A * B) / gcdAB;
 
    const numMultiplesA = Math.floor((N - 1) / A);
    const numMultiplesB = Math.floor((N - 1) / B);
    const numMultiplesLCM = Math.floor((N - 1) / lcmAB);
 
    const sumMultiplesA = (A * numMultiplesA * (numMultiplesA + 1)) / 2;
    const sumMultiplesB = (B * numMultiplesB * (numMultiplesB + 1)) / 2;
    const sumMultiplesLCM = (lcmAB * numMultiplesLCM * (numMultiplesLCM + 1)) / 2;
 
    const sum = sumMultiplesA + sumMultiplesB - sumMultiplesLCM;
 
    console.log("sum =", sum);


Output

sum = 950

Time Complexity:O(1)
Space Complexity:O(1)



Last Updated : 18 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads