Count numbers in range L-R that are divisible by all of its non-zero digits
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++
#include <bits/stdc++.h>
using namespace std;
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 ;
}
int count( int l, int r)
{
int ans = 0;
for ( int i = l; i <= r; i++)
if (check(i))
ans += 1;
return ans;
}
int main()
{
int l = 10, r = 20;
cout << count(l, r);
return 0;
}
|
Java
import java.io.*;
class GFG {
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 ;
}
static int count( int l, int r)
{
int ans = 0 ;
for ( int i = l; i <= r; i++)
if (check(i))
ans += 1 ;
return ans;
}
public static void main(String args[])
{
int l = 10 , r = 20 ;
System.out.println(count( 10 , 20 ));
}
}
|
Python3
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
def count(l, r) :
ans = 0
for i in range (l, r + 1 ) :
if (check(i)) :
ans = ans + 1
return ans
l = 10
r = 20
print (count(l, r))
|
C#
using System;
class GFG {
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 ;
}
static int count( int l, int r)
{
int ans = 0;
for ( int i = l; i <= r; i++)
if (check(i))
ans += 1;
return ans;
}
public static void Main()
{
int l = 10, r = 20;
Console.WriteLine(count(l, r));
}
}
|
PHP
<?php
function check( $n )
{
$m = $n ;
while ( $n ) {
$r = $n % 10;
if ( $r > 0)
if (( $m % $r ) != 0)
return false;
$n /= 10;
}
return true;
}
function countIn( $l , $r )
{
$ans = 0;
for ( $i = $l ; $i <= $r ; $i ++)
if (check( $i ))
$ans += 1;
return $ans ;
}
$l = 10; $r = 20;
echo countIn( $l , $r );
?>
|
Javascript
<script>
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 countIn(l, r)
{
let ans = 0;
for (let i = l; i <= r; i++)
if (check(i))
ans += 1;
return ans;
}
let l = 10;
let r = 20;
document.write(countIn(l, r));
</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.
Last Updated :
23 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...