Check if N is divisible by a number which is composed of the digits from the set {A, B}
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 53Input: 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++
// CPP program to find if number N is divisible by a // number that contains only a and b as it's digits #include <bits/stdc++.h> using namespace std; // Function to check whether n is divisible // by a number whose digits are either a or b bool isDivisibleRec(int x, int a, int b, int n) { // base condition if (x > n) return false; if (n % x == 0) return true; // recursive call return (isDivisibleRec(x * 10 + a, a, b, n) || isDivisibleRec(x * 10 + b, a, b, n)); } bool isDivisible(int a, int b, int n) { // Check for all numbers beginning with 'a' or 'b' return isDivisibleRec(a, a, b, n) || isDivisibleRec(b, a, b, n); } // Driver program int main() { int a = 3, b = 5, n = 53; if (isDivisible(a, b, n)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java program to find if number N is divisible by a // number that contains only a and b as it's digits import java.util.*; class solution { // Function to check whether n is divisible // by a number whose digits are either a or b static boolean isDivisibleRec(int x, int a, int b, int n) { // base condition if (x > n) return false; if (n % x == 0) return true; // recursive call return (isDivisibleRec(x * 10 + a, a, b, n) || isDivisibleRec(x * 10 + b, a, b, n)); } static boolean isDivisible(int a, int b, int n) { // Check for all numbers beginning with 'a' or 'b' return isDivisibleRec(a, a, b, n) ||isDivisibleRec(b, a, b, n); } // Driver program 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"); } } //contributed by Arnab Kundu |
Python 3
# Python 3 program to find if number N
# is divisible by a number that contains
# only a and b as it’s digits
# Function to check whether n is divisible
# by a number whose digits are either a or b
def isDivisibleRec(x, a, b, n):
# base condition
if (x > n):
return False
if (n % x == 0):
return True
# recursive call
return (isDivisibleRec(x * 10 + a, a, b, n) or
isDivisibleRec(x * 10 + b, a, b, n))
def isDivisible(a, b, n):
# Check for all numbers beginning
# with ‘a’ or ‘b’
return (isDivisibleRec(a, a, b, n) or
isDivisibleRec(b, a, b, n))
# Driver Code
a = 3; b = 5; n = 53;
if (isDivisible(a, b, n)):
print(“Yes”)
else:
print(“No”)
# This code is contributed
# by Akanksha Rai
C#
// C# program to find if number N is // divisible by a number that contains // only a and b as it's digits using System; class GFG { // Function to check whether n is divisible // by a number whose digits are either a or b static bool isDivisibleRec(int x, int a, int b, int n) { // base condition if (x > n) return false; if (n % x == 0) return true; // recursive call return (isDivisibleRec(x * 10 + a, a, b, n) || isDivisibleRec(x * 10 + b, a, b, n)); } static bool isDivisible(int a, int b, int n) { // Check for all numbers beginning // with 'a' or 'b' return isDivisibleRec(a, a, b, n) || isDivisibleRec(b, a, b, n); } // Driver Code static public void Main () { int a = 3, b = 5, n = 53; if (isDivisible(a, b, n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by Sachin |
PHP
<?php // PHP program to find if number N is // divisible by a number that contains // only a and b as it's digits // Function to check whether n is divisible // by a number whose digits are either a or b function isDivisibleRec($x, $a, $b, $n) { // base condition if ($x > $n) return false; if ($n % $x == 0) return true; // recursive call return (isDivisibleRec($x * 10 + $a, $a, $b, $n) || isDivisibleRec($x * 10 + $b, $a, $b, $n)); } function isDivisible($a, $b, $n) { // Check for all numbers beginning // with 'a' or 'b' return isDivisibleRec($a, $a, $b, $n) || isDivisibleRec($b, $a, $b, $n); } // Driver Code $a = 3; $b = 5; $n = 53; if (isDivisible($a, $b, $n)) echo "Yes"; else echo "No"; // This code is contributed // by Akanksha Rai |
Yes
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
Recommended Posts:
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Program to check if a number is divisible by any of its digits
- Program to check if a number is divisible by sum of its digits
- Check whether sum of digits at odd places of a number is divisible by K
- Check whether product of digits at even places of a number is divisible by K
- Given a large number, check if a subsequence of digits is divisible by 8
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find N digits number which is divisible by D
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Smallest number with sum of digits as N and divisible by 10^N
- Number of digits to be removed to make a number divisible by 3
- Possible to make a divisible by 3 number using all digits in an array
- Check if a number is divisible by 41 or not
- Check if a number is divisible by 23 or not
- Check if a number is divisible by all prime divisors of another number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



