Count numbers in range L-R that are divisible by all of its non-zero digits
Last Updated :
23 May, 2022
Given a range l - r (inclusive), count the numbers that are divisible by all of its non-zero digits.
Examples:
Input : 1 9
Output : 9
Explanation:
all the numbers are divisible by
their digits in the range 1-9.
Input : 10 20
Output : 5
Explanation:
10, 11, 12, 15, 20
Approach:
1. Run a loop to generate every number from l and r.
2. Check if every non-zero digit of that number divides the number or not.
3. Keep a count of all numbers that are completely divisible by its digits.
4. Print the count of numbers.
Below is the implementation of the above approach:
C++
// C++ program to
// Count numbers in
// range L-R that are
// divisible by
// all of its non-zero
// digits
#include <bits/stdc++.h>
using namespace std;
// check if the number is
// divisible by the digits.
bool check(int n)
{
int m = n;
while (n) {
int r = n % 10;
if (r > 0)
if ((m % r) != 0)
return false;
n /= 10;
}
return true;
}
// function to calculate the
// number of numbers
int count(int l, int r)
{
int ans = 0;
for (int i = l; i <= r; i++)
if (check(i))
ans += 1;
return ans;
}
// Driver function
int main()
{
int l = 10, r = 20;
cout << count(l, r);
return 0;
}
Java
// Java program to Count
// numbers in range L-R
// that are divisible by
// all of its non-zero
// digits
import java.io.*;
class GFG {
// check if the number
// is divisible by the
// digits.
static boolean check(int n)
{
int m = n;
while (n != 0)
{
int r = n % 10;
if (r > 0)
if ((m % r) != 0)
return false;
n /= 10;
}
return true;
}
// function to calculate
// the number of numbers
static int count(int l, int r)
{
int ans = 0;
for (int i = l; i <= r; i++)
if (check(i))
ans += 1;
return ans;
}
// Driver function
public static void main(String args[])
{
int l = 10, r = 20;
System.out.println(count(10, 20));
}
}
// This code is contributed by Nikita Tiwari.
Python3
# Python 3 program
# to Count numbers in
# range L-R that are
# divisible by all of
# its non-zero digits
# check if the number is
# divisible by the digits.
def check(n) :
m = n
while (n != 0) :
r = n % 10
if (r > 0) :
if ((m % r) != 0) :
return False
n = n // 10
return True
# function to calculate the
# number of numbers
def count(l, r) :
ans = 0
for i in range(l, r+1) :
if (check(i)) :
ans = ans + 1
return ans
# Driver function
l = 10
r = 20
print(count(l, r))
# This code is contributed by Nikita Tiwari.
C#
// Java program to Count
// numbers in range L-R
// that are divisible by
// all of its non-zero
// digits
using System;
class GFG {
// check if the number
// is divisible by the
// digits.
static bool check(int n)
{
int m = n;
while (n != 0)
{
int r = n % 10;
if (r > 0)
if ((m % r) != 0)
return false;
n /= 10;
}
return true;
}
// function to calculate
// the number of numbers
static int count(int l, int r)
{
int ans = 0;
for (int i = l; i <= r; i++)
if (check(i))
ans += 1;
return ans;
}
// Driver function
public static void Main()
{
int l = 10, r = 20;
Console.WriteLine(count(l, r));
}
}
// This code is contributed by Vt_m.
PHP
<?php
// PHP program to Count numbers
// in range L-R that are
// divisible by all of its
// non-zero digits
// check if the number is
// divisible by the digits.
function check($n)
{
$m = $n;
while ($n) {
$r = $n % 10;
if ($r > 0)
if (($m % $r) != 0)
return false;
$n /= 10;
}
return true;
}
// function to calculate the
// number of numbers
function countIn($l, $r)
{
$ans = 0;
for ($i = $l; $i <= $r; $i++)
if (check($i))
$ans += 1;
return $ans;
}
// Driver function
$l = 10; $r = 20;
echo countIn($l, $r);
// This code is contributed ajit
?>
JavaScript
<script>
// Javascript program to Count numbers
// in range L-R that are
// divisible by all of its
// non-zero digits
// check if the number is
// divisible by the digits.
function check(n)
{
let m = n;
while (n) {
let r = n % 10;
if (r > 0)
if ((n % r) != 0)
return false;
n /= 10;
}
return true;
}
// function to calculate the
// number of numbers
function countIn(l, r)
{
let ans = 0;
for (let i = l; i <= r; i++)
if (check(i))
ans += 1;
return ans;
}
// Driver function
let l = 10;
let r = 20;
document.write(countIn(l, r));
// This code is contributed by sravan kumar
</script>
Output:
5
Time Complexity: O((r-l) * log10r), where r represents the upper limit of the range and l denotes the lower limit of the given range
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Similar Reads
Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3 Given two integers L and R. The task is to find the count of all even numbers in the range [L, R] whose sum of digits is divisible by 3.Examples: Input: L = 18, R = 36 Output: 4 18, 24, 30, 36 are the only numbers in the range [18, 36] which are even and whose sum of digits is divisible by 3.Input:
8 min read
Queries to count numbers from given range which are divisible by all its digits Given a 2D array arr[][] with each row of the form of a query { L, R }, the task is to count the numbers in the range [L, R] such that the number is divisible by all of its non-zero digit. Examples: Input: arr[][] ={ {1, 5}, {12, 14} } Output: 5 1 Explanation: Query1: All the numbers in the range [1
8 min read
Count of numbers in a range that does not contain the digit M and which is divisible by M. Given three integers, the lower range L, the upper range U, and a digit M. The task is to count all the numbers between L and U such that the number is divisible by M and, also, it does not contain the digit M. Examples: Input: M = 9 ,L = 16 , U = 26 Output: 1 Explanation: Within this given range ,t
5 min read
Count numbers in range [L, R] that are divisible by square root of a number Given two numbers L and R, the task is to find the count of all numbers in a given range [L, R] that are divisible by the square root of that number. The square root of a number is the value that, when multiplied by itself, gives the original number. Note: Here square root of a number represents the
9 min read
Count numbers in a range that are divisible by all array elements Given N numbers and two numbers L and R, the task is to print the count of numbers in the range [L, R] which are divisible by all the elements of the array. Examples: Input: a[] = {1, 4, 2], L = 1, R = 10 Output : 2 In range [1, 10], the numbers 4 and 8 are divisible by all the array elements. Input
8 min read
Count numbers in range whose sum of digits is divisible by XOR of digits Given integers L and R, the task for this problem is to find a number of integers in the range L to R whose sum of digits is divisible by bitwise XOR of digits. print the answer. ( L <= R <= 1018) Note: Bitwise XOR sum zero never divides any other number. Examples: Input: L = 10, R = 15Output:
15+ min read