Given two numbers m and n, count number of m digit numbers that are divisible by n.
Examples:
Input: m = 2, n = 6
Output: 15
Explanation: Two digit numbers that are divisible by 6 are 12, 18, 24, 30, 36, ….., 96.
Input: m = 3, n = 5
Output: 180
A simple solution is two try all m digit numbers. For every number, check if it is divisible by n. If yes, we increment count.
An efficient solution involves following steps.
The idea is based on the fact that starting from first divisible number, every n-th number is divisible by n.
- Find largest m digit number.
- Find largest m-1 digit number.
- Divide both number by n and subtract later from prior.
Below is the implementation of above steps.
C++
#include<bits/stdc++.h>
using namespace std;
int findCount( int m, int n)
{
int num1 = 0;
for ( int i = 0; i < m; i++)
num1 = (num1 * 10) + 9;
int num2 = 0;
for ( int i = 0; i < (m - 1); i++)
num2 = (num2 * 10) + 9;
return ((num1 / n) - (num2 / n));
}
int main()
{
int m = 2, n = 6;
printf ( "%d\n" , findCount(m, n));
return 0;
}
|
Java
class Main
{
static int findCount( int m, int n)
{
int num1 = 0 ;
for ( int i = 0 ; i < m; i++)
num1 = (num1 * 10 ) + 9 ;
int num2 = 0 ;
for ( int i = 0 ; i < (m - 1 ); i++)
num2 = (num2 * 10 ) + 9 ;
return ((num1 / n) - (num2 / n));
}
public static void main (String[] args)
{
int m = 2 , n = 6 ;
System.out.println(findCount(m, n));
}
}
|
Python3
def findCount(m, n):
num1 = 0
for i in range ( 0 , m):
num1 = (num1 * 10 ) + 9
num2 = 0
for i in range ( 0 , (m - 1 )):
num2 = (num2 * 10 ) + 9
return int ((num1 / n) - (num2 / n))
m = 2 ; n = 6
print (findCount(m, n))
|
C#
using System;
class GfG {
static int findCount( int m, int n)
{
int num1 = 0;
for ( int i = 0; i < m; i++)
num1 = (num1 * 10) + 9;
int num2 = 0;
for ( int i = 0; i < (m - 1); i++)
num2 = (num2 * 10) + 9;
return ((num1 / n) - (num2 / n));
}
public static void Main ()
{
int m = 2, n = 6;
Console.Write(findCount(m, n));
}
}
|
PHP
<?php
function findCount( $m , $n )
{
$num1 = 0;
for ( $i = 0; $i < $m ; $i ++)
$num1 = ( $num1 * 10) + 9;
$num2 = 0;
for ( $i = 0; $i < ( $m - 1); $i ++)
$num2 = ( $num2 * 10) + 9;
return (( $num1 / $n ) - ( $num2 / $n ));
}
$m = 2; $n = 6;
echo findCount( $m , $n ), "\n" ;
?>
|
Javascript
<script>
function findCount(m, n)
{
let num1 = 0;
for (let i = 0; i < m; i++)
num1 = (num1 * 10) + 9;
let num2 = 0;
for (let i = 0; i < (m - 1); i++)
num2 = (num2 * 10) + 9;
return ((num1 / n) - (num2 / n));
}
let m = 2; n = 6;
document.write(findCount(m, n) + "<br>" );
</script>
|
Output :
15
Time complexity: O(m)
Auxiliary space: O(1) as it is using constant space for variables
This article is contributed by Aditya Kumar. 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.