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 5



Input: 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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..

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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    
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 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 
def sumAP(n, d):
      
    # Number of terms 
    n = n//
  
    return (n) * (1 + n) * d // 2
  
# Function to find the sum of all 
# multiples of a and b below n 
def sumMultiples(n, a, b):
  
    # Since, we need the sum of 
    # multiples less than N 
    n = n-1
    return sumAP(n, a) + sumAP(n, b) - \
                         sumAP(n, a * b) 
  
# Driver code 
n = 10
a = 3
b = 5
print(sumMultiples(n, a, b)) 
  
# This code is contributed by Sanjit Prasad

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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. 
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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. 

chevron_right


Output:

23


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.