A and B are two numbers which define a range, where A <= B. Find the total numbers in the given range [A … B] divisible by ‘M’
Examples:
Input : A = 25, B = 100, M = 30
Output : 3
Explanation : In the given range [25 - 100],
30, 60 and 90 are divisible by 30
Input : A = 6, B = 15, M = 3
Output : 4
Explanation : In the given range [6 - 15],
6, 9, 12 and 15 are divisible by 3
Method 1 : [Brute-force]
Run a loop from A to B. If a number divisible by ‘M’ is found, increment counter.
Below is the implementation of above method:
C++
#include <bits/stdc++.h>
using namespace std;
int countDivisibles( int A, int B, int M)
{
int counter = 0;
for ( int i = A; i <= B; i++)
if (i % M == 0)
counter++;
return counter;
}
int main()
{
int A = 30, B = 100, M = 30;
cout << countDivisibles(A, B, M) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countDivisibles( int A, int B, int M)
{
int counter = 0 ;
for ( int i = A; i <= B; i++)
if (i % M == 0 )
counter++;
return counter;
}
public static void main(String[] args)
{
int A = 30 , B = 100 , M = 30 ;
System.out.println(countDivisibles(A, B, M));
}
}
|
Python3
def countDivisibles(A, B, M):
counter = 0 ;
for i in range (A, B):
if (i % M = = 0 ):
counter = counter + 1
return counter
A = 30
B = 100
M = 30
print (countDivisibles(A, B, M))
|
C#
using System;
public class GFG {
static int countDivisibles( int A, int B, int M)
{
int counter = 0;
for ( int i = A; i <= B; i++)
if (i % M == 0)
counter++;
return counter;
}
public static void Main()
{
int A = 30, B = 100, M = 30;
Console.WriteLine(countDivisibles(A, B, M));
}
}
|
PHP
<?php
function countDivisibles( $A , $B , $M )
{
$counter = 0;
for ( $i = $A ; $i <= $B ; $i ++)
if ( $i % $M == 0)
$counter ++;
return $counter ;
}
$A = 30;
$B = 100;
$M = 30;
echo countDivisibles( $A , $B , $M ), "\n" ;
?>
|
Javascript
<script>
function countDivisibles(A, B, M)
{
let counter = 0;
for (let i = A; i <= B; i++)
if (i % M == 0)
counter++;
return counter;
}
let A = 30;
let B = 100;
let M = 30;
document.write(countDivisibles(A, B, M));
</script>
|
Output:
3
Time Complexity: O(B)
Auxiliary Space: O(1)
Method 2 : [Better]
The loop can be modified by incrementing the iterator ‘M’ times after the first divisible is found. Also, if ‘A’ is less than ‘M’, it can be changed to ‘M’, because a number less than ‘M’ can not be divided by it.
Method 3 : [Efficient]
Let B = b * M and
A = a * M
The count of numbers divisible by
'M' between A and B will be equal
to b - a.
Example:
A = 25, B = 70, M = 10.
Now, a = 2, b = 7.
Count = 7 - 2 = 5.
It can be observed that, if A is divisible by M, ‘b – a’ will exclude the count for A, so the count will be less by 1. Thus, in this case we add 1 explicitly.
Example when A is divisible by M:
A = 30, B = 70, M = 10.
Now, a = 3, b = 7.
Count = 7 - 3 = 4.
But, Count should be 5. Thus, we will
add 1 explicitly.
Below is the implementation of the above method :
C++
#include <bits/stdc++.h>
using namespace std;
int countDivisibles( int A, int B, int M)
{
if (A % M == 0)
return (B / M) - (A / M) + 1;
return (B / M) - (A / M);
}
int main()
{
int A = 30, B = 100, M = 30;
cout << (countDivisibles(A, B, M));
}
|
Java
import java.io.*;
class GFG {
static int countDivisibles( int A, int B, int M)
{
if (A % M == 0 )
return (B / M) - (A / M) + 1 ;
return (B / M) - (A / M);
}
public static void main(String[] args)
{
int A = 30 , B = 100 , M = 30 ;
System.out.println(countDivisibles(A, B, M));
}
}
|
Python3
def countDivisibles(A, B, M):
if (A % M = = 0 ):
return ((B / M) - (A / M)) + 1
return ((B / M) - (A / M))
A = 30
B = 70
M = 10
print (countDivisibles(A, B, M))
|
C#
using System;
public class GFG {
static int countDivisibles( int A, int B, int M)
{
if (A % M == 0)
return (B / M) - (A / M) + 1;
return (B / M) - (A / M);
}
public static void Main()
{
int A = 30, B = 100, M = 30;
Console.WriteLine(countDivisibles(A, B, M));
}
}
|
PHP
<?php
function countDivisibles( $A , $B , $M )
{
if ( $A % $M == 0)
return ( $B / $M ) -
( $A / $M ) + 1;
return ( $B / $M ) -
( $A / $M );
}
$A = 30;
$B = 70;
$M = 10;
echo countDivisibles( $A , $B , $M ) ;
return 0;
?>
|
Javascript
function countDivisibles(A, B, M)
{
if (A % M == 0)
return (B / M) -
(A / M) + 1;
return (B / M) -
(A / M);
}
let A = 30;
let B = 70;
let M = 10;
document.write(countDivisibles(A, B, M));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.