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
A naive approach is to iterate through 1 to and find the multiples of A and B and add them to sum. At the end of the loop display the sum.
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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll sumAP(ll n, ll d)
{
n /= d;
return (n) * (1 + n) * d / 2;
}
ll sumMultiples(ll A, ll B, ll n)
{
n--;
ll common = (A * B) / __gcd(A, B);
return sumAP(n, A) + sumAP(n, B) - sumAP(n, common);
}
int main()
{
ll n = 100, A = 5, B = 10;
cout << "Sum = " << sumMultiples(A, B, n);
return 0;
}
|
Java
class GFG{
static int __gcd( int a, int b)
{
if (b == 0 )
return a;
return __gcd(b, a % b);
}
static int sumAP( int n, int d)
{
n /= d;
return (n) * ( 1 + n) * d / 2 ;
}
static int sumMultiples( int A, int B, int n)
{
n--;
int common = (A * B) / __gcd(A,B);
return sumAP(n, A) + sumAP(n, B) - sumAP(n, common);
}
public static void main(String[] args)
{
int n = 100 , A = 5 , B = 10 ;
System.out.println( "Sum = " +sumMultiples(A, B, n));
}
}
|
Python3
from math import gcd,sqrt
def sumAP(n, d):
n = int (n / d)
return (n) * ( 1 + n) * d / 2
def sumMultiples(A, B, n):
n - = 1
common = int ((A * B) / gcd(A, B))
return (sumAP(n, A) + sumAP(n, B) -
sumAP(n, common))
if __name__ = = '__main__' :
n = 100
A = 5
B = 10
print ( "Sum =" , int (sumMultiples(A, B, n)))
|
C#
class GFG{
static int __gcd( int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
static int sumAP( int n, int d)
{
n /= d;
return (n) * (1 + n) * d / 2;
}
static int sumMultiples( int A, int B, int n)
{
n--;
int common = (A * B) / __gcd(A,B);
return sumAP(n, A) + sumAP(n, B) - sumAP(n, common);
}
public static void Main()
{
int n = 100, A = 5, B = 10;
System.Console.WriteLine( "Sum = " +sumMultiples(A, B, n));
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $b == 0)
return $a ;
return __gcd( $b , $a % $b );
}
function sumAP( $n , $d )
{
$n = (int)( $n / $d );
return ( $n ) * (1 + $n ) * $d / 2;
}
function sumMultiples( $A , $B , $n )
{
$n --;
$common = (int)(( $A * $B ) /
__gcd( $A , $B ));
return sumAP( $n , $A ) +
sumAP( $n , $B ) -
sumAP( $n , $common );
}
$n = 100;
$A = 5;
$B = 10;
echo "Sum = " . sumMultiples( $A , $B , $n );
?>
|
Javascript
<script>
function __gcd(a,b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
function sumAP(n, d)
{
n = parseInt(n / d);
return (n) * (1 + n) * d / 2;
}
function sumMultiples(A, B, n)
{
n--;
common = parseInt((A * B) /
__gcd(A, B));
return sumAP(n, A) +
sumAP(n, B) -
sumAP(n, common);
}
let n = 100;
let A = 5;
let B = 10;
document.write( "Sum = " + sumMultiples(A, B, n));
</script>
|