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++
#include <iostream>
using namespace std;
int findSum( int n, int a, int b)
{
int sum = 0;
for ( int i = 0; i < n; i++)
if (i % a == 0 || i % b == 0)
sum += i;
return sum;
}
int main()
{
int n = 10, a = 3, b = 5;
cout << findSum(n, a, b);
return 0;
}
|
C
#include <stdio.h>
int findSum( int n, int a, int b)
{
int sum = 0;
for ( int i = 0; i < n; i++)
if (i % a == 0 || i % b == 0)
sum += i;
return sum;
}
int main()
{
int n = 10, a = 3, b = 5;
printf ( "%d" ,findSum(n, a, b));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int findSum( int n, int a, int b)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
if (i % a == 0 || i % b == 0 )
sum += i;
return sum;
}
public static void main(String[] args)
{
int n = 10 , a = 3 , b = 5 ;
System.out.println(findSum(n, a, b));
}
}
|
Python3
def findSum(n, a, b):
sum = 0
for i in range ( 0 , n, 1 ):
if (i % a = = 0 or i % b = = 0 ):
sum + = i
return sum
if __name__ = = '__main__' :
n = 10
a = 3
b = 5
print (findSum(n, a, b))
|
C#
using System;
class GFG
{
static int findSum( int n, int a, int b)
{
int sum = 0;
for ( int i = 0; i < n; i++)
if (i % a == 0 || i % b == 0)
sum += i;
return sum;
}
static void Main()
{
int n = 10, a = 3, b = 5;
Console.WriteLine(findSum(n, a, b));
}
}
|
PHP
<?php
function findSum( $n , $a , $b )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
if ( $i % $a == 0 || $i % $b == 0)
$sum += $i ;
return $sum ;
}
$n = 10;
$a = 3;
$b = 5;
echo findSum( $n , $a , $b );
?>
|
Javascript
<script>
function findSum(n, a, b)
{
let sum = 0;
for (let i = 0; i < n; i++)
if (i % a == 0 || i % b == 0)
sum += i;
return sum;
}
let n = 10;
let a = 3;
let b = 5;
document.write( findSum(n, a, b));
</script>
|
Time Complexity: O(N), since the loop runs from 0 to (n – 1).
Auxiliary Space: O(1), since no extra space has been taken.
Efficient approach:
For better understanding of an efficient approach let us start from the scratch-
We have the numbers = 1, 2, 3, 4, ………. , N-1 , N
All the numbers divisible by A = A, 2A, 3A, ………….. ?N/A?*A
Let us call this , sum1 =A + 2A + 3A+ …………..+ ?N/A?*A
sum1 = A(1 + 2 + 3+ …………..+ ?N/A? )
sum1 = A* ?N/A? * ( ?N/A? + 1 )/2
Where ? ? is floor (or Least Integer) function .
and sum of n natural numbers formulae n*(n+1)/2 is used.
Similarly sum of numbers divisible by B –
sum2 =B* ?N/B? *( ?N/B? + 1 )/2
So total sum = sum1 + sum2 but there may be some numbers that will be common in both,
For example let N=10, A=2, B=3
Then sum1 = 2+4+6+8+10+12+14+16+18+20
sum2 = 3+6+9+12+15+18
We can clearly see numbers 6, 12, 18 are repeated and all other numbers are multiple of 6 that is the LCM of A and B
Let lcm = LCM of A and B
sum3 =lcm* ?N/lcm? * ( ?N/lcm? + 1 )/2
At last we can calculate the sum by using
sum = sum1 + sum2 – sum3
we can calculate the LCM by using
lcm = (A*B)/gcd
where gcd = GCD of A and B
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
long long sumAP( long long n, long long d)
{
n /= d;
return (n) * (1 + n) * d / 2;
}
long long sumMultiples( long long n, long long a,
long long b)
{
n--;
long lcm = (a*b)/__gcd(a,b);
return sumAP(n, a) + sumAP(n, b) -
sumAP(n, lcm);
}
int main()
{
long long n = 10, a = 3, b = 5;
cout << sumMultiples(n, a, b);
return 0;
}
|
C
#include <stdio.h>
long long sumAP( long long n, long long d)
{
n /= d;
return (n) * (1 + n) * d / 2;
}
long gcd( int p, int q)
{
if (p == 0)
return q;
return gcd(q % p, p);
}
long long sumMultiples( long long n, long long a,
long long b)
{
n--;
long lcm = (a*b)/gcd(a,b);
return sumAP(n, a) + sumAP(n, b) - sumAP(n, lcm);
}
int main()
{
long long n = 10, a = 3, b = 5;
printf ( "%lld" , sumMultiples(n, a, b));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static long sumAP( long n, long d)
{
n = ( int )n / d;
return (n) * ( 1 + n) * d / 2 ;
}
public static long gcd( long p, long q)
{
if (p == 0 )
return q;
return gcd(q % p, p);
}
static long sumMultiples( long n, long a,
long b)
{
n--;
long lcm = (a * b) / gcd(a, b);
return sumAP(n, a) + sumAP(n, b) -
sumAP(n, lcm);
}
public static void main(String[] args)
{
long n = 10 , a = 3 , b = 5 ;
System.out.println(sumMultiples(n, a, b));
}
}
|
Python3
import math
def sumAP(n, d):
n = n / / d
return (n) * ( 1 + n) * d / / 2
def sumMultiples(n, a, b):
n = n - 1
lcm = (a * b) / / math.gcd(a, b)
return sumAP(n, a) + sumAP(n, b) - \
sumAP(n, lcm)
n = 10
a = 3
b = 5
print (sumMultiples(n, a, b))
|
C#
using System;
public class GFG
{
static long sumAP( long n, long d)
{
n = ( int )n / d;
return (n) * (1 + n) * d / 2;
}
static long gcd( long p, long q)
{
if (p == 0)
return q;
return gcd(q % p, p);
}
static long sumMultiples( long n, long a,
long b)
{
n--;
long lcm = (a * b) / gcd(a, b);
return sumAP(n, a) + sumAP(n, b) -
sumAP(n, lcm);
}
static public void Main()
{
long n = 10, a = 3, b = 5;
Console.WriteLine(sumMultiples(n, a, b));
}
}
|
PHP
<?php
function sumAP( $n , $d )
{
$n = (int)( $n / $d );
return ( $n ) * (1 + $n ) * $d / 2;
}
function sumMultiples( $n , $a , $b )
{
$n --;
return sumAP( $n , $a ) + sumAP( $n , $b ) -
sumAP( $n , $a * $b );
}
{
$n = 10;
$a = 3;
$b = 5;
echo (sumMultiples( $n , $a , $b ));
return 0;
}
|
Javascript
<script>
function sumAP(n, d)
{
n = parseInt(n / d);
return (n) * (1 + n) * d / 2;
}
function gcd(p, q)
{
if (p == 0)
return q;
return gcd(q % p, p);
}
function sumMultiples(n, a, b)
{
n--;
var lcm = (a * b) / gcd(a, b);
return sumAP(n, a) + sumAP(n, b) -
sumAP(n, lcm);
}
{
let n = 10;
let a = 3;
let b = 5;
document.write(sumMultiples(n, a, b));
}
</script>
|
Time Complexity: O(log(N))
Auxiliary Space: O(1)
Last Updated :
29 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...