- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 −

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

- Related Questions & Answers
- Find N digits number which is divisible by D in C++
- C Program to check if a number is divisible by sum of its digits
- C Program to check if a number is divisible by any of its digits
- Check if any permutation of a large number is divisible by 8 in Python
- Check if any permutation of a number is divisible by 3 and is Palindromic in Python
- Add N digits to A such that it is divisible by B after each addition?
- Check if a large number is divisible by 20 in C++
- Check if the frequency of all the digits in a number is same in Python
- Check whether product of digits at even places of a number is divisible by K in Python
- Check whether sum of digits at odd places of a number is divisible by K in Python
- Sum which is divisible by n in JavaScript
- Check if a number is divisible by 23 or not in C++
- Check if a number is divisible by 41 or not in C++
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- Check if a number is divisible by all prime divisors of another number in C++

Advertisements