Given two numbers m and n, count number of m digit numbers that are divisible by n.
Input : m = 2 n = 6 Output : 15 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.
Time complexity : O(m)
This article is contributed by Aditya Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Sum of last digit of all integers from 1 to N divisible by M
- Count 'd' digit positive integers with 0 as a digit
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count integers in the range [A, B] that are not divisible by C and D
- Count positive integers with 0 as a digit and maximum 'd' digits
- Count integers in a range which are divisible by their euler totient value
- Count of N digit palindromic numbers divisible by 9
- Count n digit numbers divisible by given number
- Count of numbers in range which are divisible by M and have digit D at odd places
- Minimum decrements to make integer A divisible by integer B
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Sum of integers upto N with given unit digit (Set 2)
- Sum of integers upto N with given unit digit
- Number of N digit integers with weight W