Check if N is divisible by a number which is composed of the digits from the set {A, B}
Last Updated :
09 Sep, 2022
Given three integers N, A and B, the task is to find whether N is divisible by any number that contains only A and B as it’s digits.
Examples:
Input: N = 106, a = 3, b = 5
Output: Yes
106 is divisible by 53
Input: N = 107, a = 3, b = 5
Output: No
Approach 1 (Recursive):
An efficient solution is to make all the numbers that contains a and b as their digits using recursive function starting with the numbers a and b. If function call is fun(x) then recursively call for fun(x * 10 + a) and fun(x * 10 + b). If n is divisible by any of the number then print Yes else print No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isDivisibleRec( int x, int a, int b, int n)
{
if (x > n)
return false ;
if (n % x == 0)
return true ;
return (isDivisibleRec(x * 10 + a, a, b, n)
|| isDivisibleRec(x * 10 + b, a, b, n));
}
bool isDivisible( int a, int b, int n)
{
return isDivisibleRec(a, a, b, n) ||
isDivisibleRec(b, a, b, n);
}
int main()
{
int a = 3, b = 5, n = 53;
if (isDivisible(a, b, n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class solution
{
static boolean isDivisibleRec( int x, int a, int b, int n)
{
if (x > n)
return false ;
if (n % x == 0 )
return true ;
return (isDivisibleRec(x * 10 + a, a, b, n)
|| isDivisibleRec(x * 10 + b, a, b, n));
}
static boolean isDivisible( int a, int b, int n)
{
return isDivisibleRec(a, a, b, n)
||isDivisibleRec(b, a, b, n);
}
public static void main(String args[])
{
int a = 3 , b = 5 , n = 53 ;
if (isDivisible(a, b, n))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def isDivisibleRec(x, a, b, n):
if (x > n):
return False
if (n % x = = 0 ):
return True
return (isDivisibleRec(x * 10 + a, a, b, n) or
isDivisibleRec(x * 10 + b, a, b, n))
def isDivisible(a, b, n):
return (isDivisibleRec(a, a, b, n) or
isDivisibleRec(b, a, b, n))
a = 3 ; b = 5 ; n = 53 ;
if (isDivisible(a, b, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isDivisibleRec( int x, int a,
int b, int n)
{
if (x > n)
return false ;
if (n % x == 0)
return true ;
return (isDivisibleRec(x * 10 + a, a, b, n) ||
isDivisibleRec(x * 10 + b, a, b, n));
}
static bool isDivisible( int a, int b, int n)
{
return isDivisibleRec(a, a, b, n) ||
isDivisibleRec(b, a, b, n);
}
static public void Main ()
{
int a = 3, b = 5, n = 53;
if (isDivisible(a, b, n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function isDivisibleRec( $x , $a , $b , $n )
{
if ( $x > $n )
return false;
if ( $n % $x == 0)
return true;
return (isDivisibleRec( $x * 10 + $a , $a , $b , $n ) ||
isDivisibleRec( $x * 10 + $b , $a , $b , $n ));
}
function isDivisible( $a , $b , $n )
{
return isDivisibleRec( $a , $a , $b , $n ) ||
isDivisibleRec( $b , $a , $b , $n );
}
$a = 3; $b = 5; $n = 53;
if (isDivisible( $a , $b , $n ))
echo "Yes" ;
else
echo "No" ;
|
Javascript
<script>
function isDivisibleRec(x, a, b, n)
{
if (x > n)
return false ;
if (n % x == 0)
return true ;
return (isDivisibleRec(x * 10 + a, a, b, n) ||
isDivisibleRec(x * 10 + b, a, b, n));
}
function isDivisible(a, b, n)
{
return isDivisibleRec(a, a, b, n) ||
isDivisibleRec(b, a, b, n);
}
let a = 3, b = 5, n = 53;
if (isDivisible(a, b, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Approach 2 (Queue Based):
The idea is to generate all numbers (smaller than n) containing digits a and b. For every number check if it divides n or not. How to generate all numbers smaller than n? We use queue for this. Initially we push ‘a’ and ‘b’ to the queue. Then we run a loop while front of queue is smaller than n. We pop an item one by one and for ever popped item x, we generate next numbers x*10 + a and x*10 + b and enqueue them. Time complexity of this approach is O(n).
Please refer below post for implementation of this approach.
Count of Binary Digit numbers smaller than N
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...