# Check if N is divisible by a number which is composed of the digits from the set {A, B} in Python

Suppose we have a number n. We have another two numbers a and b. We have to check whether we can generate a number using a and b that divides n.

So, if the input is like n = 115, a = 3, b = 2, then the output will be True as 115 is divisible by 23 which is made of 2 and 3.

To solve this, we will follow these steps −

• Define a function util() . This will take temp, a, b, n
• if temp > n, then
• return False
• if n is divisible by temp, then

• return True
• return true when at least one of util(temp * 10 + a, a, b, n) or util(temp * 10 + b, a, b, n) is true otherwise false
• From the main method return true when at least one of util(a, a, b, n) or util(b, a, b, n) is true otherwise false

## Example

Let us see the following implementation to get better understanding −

Live Demo

def util(temp, a, b, n):
if temp > n:
return False
if n % temp == 0:
return True
return util(temp * 10 + a, a, b, n) or util(temp * 10 + b, a, b, n)
def solve(n, a, b):
return util(a, a, b, n) or util(b, a, b, n)
n = 115
a = 3
b = 2
print(solve(n, a, b))

## Input

115, 2, 3

## Output

True