Given three numbers A, B and M such that A < B, the task is to find the sum of numbers divisible by M in the range [A, B].
Examples:
Input: A = 25, B = 100, M = 30
Output: 180
Explanation:
In the given range [25, 100] 30, 60 and 90 are the numbers which are divisible by M = 30
Therefore, sum of these numbers = 180.
Input: A = 6, B = 15, M = 3
Output: 42
Explanation:
In the given range [6, 15] 6, 9, 12 and 15 are the numbers which are divisible by M = 3.
Therefore, sum of these numbers = 42.
Naive Approach: Check for each number in the range [A, B] if they are divisible by M or not. And finally, add all the numbers that are divisible by M.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sumDivisibles( int A, int B, int M)
{
int sum = 0;
for ( int i = A; i <= B; i++)
if (i % M == 0)
sum += i;
return sum;
}
int main()
{
int A = 6, B = 15, M = 3;
cout << sumDivisibles(A, B, M) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int sumDivisibles( int A, int B, int M)
{
int sum = 0 ;
for ( int i = A; i <= B; i++)
if (i % M == 0 )
sum += i;
return sum;
}
public static void main(String[] args)
{
int A = 6 , B = 15 , M = 3 ;
System.out.print(sumDivisibles(A, B, M) + "\n" );
}
}
|
Python3
def sumDivisibles(A, B, M):
sum = 0
for i in range (A, B + 1 ):
if (i % M = = 0 ):
sum + = i
return sum
if __name__ = = "__main__" :
A = 6
B = 15
M = 3
print (sumDivisibles(A, B, M))
|
C#
using System;
class GFG{
static int sumDivisibles( int A, int B, int M)
{
int sum = 0;
for ( int i = A; i <= B; i++)
if (i % M == 0)
sum += i;
return sum;
}
public static void Main(String[] args)
{
int A = 6, B = 15, M = 3;
Console.Write(sumDivisibles(A, B, M) + "\n" );
}
}
|
Javascript
<script>
function sumDivisibles(A, B, M)
{
var sum = 0;
for ( var i = A; i <= B; i++)
if (i % M == 0)
sum += i;
return sum;
}
var A = 6, B = 15, M = 3;
document.write(sumDivisibles(A, B, M));
</script>
|
Time Complexity: O(B-A).
Auxiliary Space: O(1)
Efficient Approach: The idea is to use the concept of Arithmetic Progression and divisibility.
- Upon visualization, the multiples of M can be seen to form a series
M, 2M, 3M, ...
- If we can find the value of K which is the first term in the range [A, B] which is divisible by M, then directly, the series would be:
K, (K + M), (K + 2M), ------ (K + (N - 1)*M )
where N is the number of elements in the series.
N = B / M - (A - 1)/ M
- Therefore, the sum of the elements can be found out by:
sum = N * ( (first term + last term) / 2)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSmallNum( int N, int K)
{
int rem = N % K;
if (rem == 0)
return N;
else
return N - rem;
}
int findLargeNum( int N, int K)
{
int rem = (N + K) % K;
if (rem == 0)
return N;
else
return N + K - rem;
}
int sumDivisibles( int A, int B, int M)
{
int sum = 0;
int first = findSmallNum(A, M);
int last = findLargeNum(B, M);
if (first < A)
first += M;
if (last > B)
first -= M;
int n = (B / M) - (A - 1) / M;
return n * (first + last) / 2;
}
int main()
{
int A = 6, B = 15, M = 3;
cout << sumDivisibles(A, B, M);
return 0;
}
|
Java
class GFG{
static int findSmallNum( int N, int K)
{
int rem = N % K;
if (rem == 0 )
return N;
else
return N - rem;
}
static int findLargeNum( int N, int K)
{
int rem = (N + K) % K;
if (rem == 0 )
return N;
else
return N + K - rem;
}
static int sumDivisibles( int A, int B, int M)
{
int first = findSmallNum(A, M);
int last = findLargeNum(B, M);
if (first < A)
first += M;
if (last > B)
first -= M;
int n = (B / M) - (A - 1 ) / M;
return n * (first + last) / 2 ;
}
public static void main(String[] args)
{
int A = 6 , B = 15 , M = 3 ;
System.out.print(sumDivisibles(A, B, M));
}
}
|
Python3
def findSmallNum(N, K):
rem = N % K
if (rem = = 0 ):
return N
else :
return N - rem
def findLargeNum(N, K):
rem = (N + K) % K
if (rem = = 0 ):
return N
else :
return N + K - rem
def sumDivisibles(A, B, M):
sum = 0
first = findSmallNum(A, M)
last = findLargeNum(B, M)
if (first < A):
first + = M
if (last > B):
first - = M
n = (B / / M) - (A - 1 ) / / M
return n * (first + last) / / 2
if __name__ = = '__main__' :
A = 6
B = 15
M = 3
print (sumDivisibles(A, B, M))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int findSmallNum( int N, int K)
{
int rem = N % K;
if (rem == 0)
return N;
else
return N - rem;
}
static int findLargeNum( int N, int K)
{
int rem = (N + K) % K;
if (rem == 0)
return N;
else
return N + K - rem;
}
static int sumDivisibles( int A, int B, int M)
{
int first = findSmallNum(A, M);
int last = findLargeNum(B, M);
if (first < A)
first += M;
if (last > B)
first -= M;
int n = (B / M) - (A - 1) / M;
return n * (first + last) / 2;
}
public static void Main(String[] args)
{
int A = 6, B = 15, M = 3;
Console.Write(sumDivisibles(A, B, M));
}
}
|
Javascript
<script>
function findSmallNum(N, K)
{
var rem = N % K;
if (rem == 0)
return N;
else
return N - rem;
}
function findLargeNum(N, K)
{
var rem = (N + K) % K;
if (rem == 0)
return N;
else
return N + K - rem;
}
function sumDivisibles(A, B, M)
{
var sum = 0;
var first = findSmallNum(A, M);
var last = findLargeNum(B, M);
if (first < A)
first += M;
if (last > B)
first -= M;
var n = (parseInt(B / M) -
parseInt((A - 1) / M));
return n * (first + last) / 2;
}
var A = 6, B = 15, M = 3;
document.write( sumDivisibles(A, B, M));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)