# Smallest N digit number with none of its digits as its divisor

Given an integer N. The task is to find the smallest N digit number S, such that S is not divisible by any of its digits. Print -1 if no such number is possible.

Examples:

Input: N = 2
Output: 23
Explanation: 23 is the smallest two digit number that is not divisible by any of its digits.

Input: N = 1
Output: -1
Explanation: Every single digit number is divisible by itself.

Input: N = 4
Output: 2227
Explanation: 2227 is the smallest four digit number that is not divisible by any of its digits.

Approach :

• The approach is to find the smallest and largest possible N-digit number say these numbers are L and R respectively.
• Iterate over the range [L, R].
• For each number in the range, extract every digit of that number and check if the number is divisible by that digit.
• If at least one digit of the number divides it, discard that number and iterate to check the next number.
• If none of the digits of that number divides it, then print that number and break out of the loop. This number so found is the smallest number.

Below is the implementation of the above approach:

## C++

 `// C++ Program for the given problem  ` `#include   ` `using` `namespace` `std;  ` ` `  `// Function to calculate power  ` `int` `power(``int` `a, ``int` `b)  ` `{  ` `    ``if` `(b == 0)  ` `        ``return` `1;  ` `    ``if` `(b == 1)  ` `        ``return` `a;  ` ` `  `    ``int` `tmp = power(a, b / 2);  ` ` `  `    ``int` `result = tmp * tmp;  ` ` `  `    ``if` `(b % 2 == 1)  ` `        ``result *= a;  ` ` `  `    ``return` `result;  ` `}  ` ` `  `// Function to check if the  ` `// N-digit number satifies the  ` `// given condition or not  ` `bool` `check(``int` `n)  ` `{  ` `    ``int` `temp = n;  ` `    ``while` `(temp > 0) {  ` ` `  `        ``// Getting the last digit  ` `        ``int` `last_digit = temp % 10;  ` ` `  `        ``// Every number is divisible  ` `        ``// by 1 and dividing a number  ` `        ``// by 0 isn't possible. Thus  ` `        ``// numbers with 0 as a digit  ` `        ``// must be discarded.  ` `        ``if` `(last_digit == 0  ` `            ``|| last_digit == 1)  ` `            ``return` `false``;  ` ` `  `        ``// If any digit divides the  ` `        ``// number, return false  ` `        ``if` `(n % last_digit == 0)  ` `            ``return` `false``;  ` `        ``temp = temp / 10;  ` `    ``}  ` ` `  `    ``// If no digit divides the  ` `    ``// number, return true;  ` `    ``return` `true``;  ` `}  ` ` `  `// Function to find the smallest  ` `// number not divisible by any  ` `// of its digits.  ` `void` `solve(``int` `n)  ` `{  ` `    ``// Get the lower range of n  ` `    ``// digit number  ` `    ``int` `L = power(10, n - 1);  ` ` `  `    ``// Get the high range of n  ` `    ``// digit number  ` `    ``int` `R = power(10, n) - 1;  ` ` `  `    ``int` `flag = 0;  ` ` `  `    ``// check all the N-digit numbers  ` `    ``for` `(``int` `i = L; i <= R; i++) {  ` ` `  `        ``bool` `answer = check(i);  ` ` `  `        ``// If the first number to satify  ` `        ``// the constraints is found  ` `        ``// print it, set the flag value  ` `        ``// and break out of the loop.  ` `        ``if` `(answer == ``true``) {  ` ` `  `            ``cout << i << endl;  ` `            ``flag++;  ` `            ``break``;  ` `        ``}  ` `    ``}  ` ` `  `    ``// If no such digit found,  ` `    ``// return -1 as per problem statement  ` `    ``if` `(flag == 0)  ` `        ``cout << -1 << endl;  ` `}  ` ` `  `// Driver Code  ` `int` `main()  ` `{  ` `    ``int` `N = 4;  ` `    ``solve(N);  ` `}  `

## Java

 `// Java program for the given problem  ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `     `  `// Function to calculate power ` `static` `int` `power(``int` `a, ``int` `b) ` `{ ` `    ``if` `(b == ``0``) ` `        ``return` `1``; ` `    ``if` `(b == ``1``) ` `        ``return` `a; ` `         `  `    ``int` `tmp = power(a, b / ``2``); ` `    ``int` `result = tmp * tmp; ` `     `  `    ``if` `(b % ``2` `== ``1``) ` `        ``result *= a; ` ` `  `    ``return` `result; ` `} ` ` `  `// Function to check if the ` `// N-digit number satifies the ` `// given condition or not ` `static` `boolean` `check(``int` `n) ` `{ ` `    ``int` `temp = n; ` `    ``while` `(temp > ``0``) ` `    ``{ ` `         `  `        ``// Getting the last digit ` `        ``int` `last_digit = temp % ``10``; ` `         `  `        ``// Every number is divisible ` `        ``// by 1 and dividing a number ` `        ``// by 0 isn't possible. Thus ` `        ``// numbers with 0 as a digit ` `        ``// must be discarded. ` `        ``if` `(last_digit == ``0` `|| ` `            ``last_digit == ``1``) ` `            ``return` `false``; ` `             `  `        ``// If any digit divides the ` `        ``// number, return false ` `        ``if` `(n % last_digit == ``0``) ` `            ``return` `false``; ` `        ``temp = temp / ``10``; ` `    ``} ` `     `  `    ``// If no digit divides the ` `    ``// number, return true; ` `    ``return` `true``; ` `} ` ` `  `// Function to find the smallest ` `// number not divisible by any ` `// of its digits. ` `static` `void` `solve(``int` `n) ` `{ ` `     `  `    ``// Get the lower range of n ` `    ``// digit number ` `    ``int` `L = power(``10``, n - ``1``); ` `     `  `    ``// Get the high range of n ` `    ``// digit number ` `    ``int` `R = power(``10``, n) - ``1``; ` `     `  `    ``int` `flag = ``0``; ` `     `  `    ``// Check all the N-digit numbers ` `    ``for``(``int` `i = L; i <= R; i++)  ` `    ``{ ` `        ``boolean` `answer = check(i); ` `         `  `        ``// If the first number to satify ` `        ``// the constraints is found ` `        ``// print it, set the flag value ` `        ``// and break out of the loop. ` `        ``if` `(answer == ``true``) ` `        ``{ ` `            ``System.out.println(i); ` `            ``flag++; ` `            ``break``; ` `        ``} ` `    ``} ` `     `  `    ``// If no such digit found, ` `    ``// return -1 as per problem statement ` `    ``if` `(flag == ``0``) ` `    ``System.out.println(``"-1"``); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `N = ``4``; ` `     `  `    ``solve(N); ` `} ` `} ` ` `  `// This code is contributed by offbeat `

## C#

 `// C# program for the given problem  ` `using` `System; ` ` `  `class` `GFG{ ` `     `  `// Function to calculate power ` `static` `int` `power(``int` `a, ``int` `b) ` `{ ` `    ``if` `(b == 0) ` `        ``return` `1; ` `    ``if` `(b == 1) ` `        ``return` `a; ` `         `  `    ``int` `tmp = power(a, b / 2); ` `    ``int` `result = tmp * tmp; ` `     `  `    ``if` `(b % 2 == 1) ` `        ``result *= a; ` ` `  `    ``return` `result; ` `} ` ` `  `// Function to check if the ` `// N-digit number satifies the ` `// given condition or not ` `static` `bool` `check(``int` `n) ` `{ ` `    ``int` `temp = n; ` `    ``while` `(temp > 0) ` `    ``{ ` `         `  `        ``// Getting the last digit ` `        ``int` `last_digit = temp % 10; ` `         `  `        ``// Every number is divisible ` `        ``// by 1 and dividing a number ` `        ``// by 0 isn't possible. Thus ` `        ``// numbers with 0 as a digit ` `        ``// must be discarded. ` `        ``if` `(last_digit == 0 || ` `            ``last_digit == 1) ` `            ``return` `false``; ` `             `  `        ``// If any digit divides the ` `        ``// number, return false ` `        ``if` `(n % last_digit == 0) ` `            ``return` `false``; ` `        ``temp = temp / 10; ` `    ``} ` `     `  `    ``// If no digit divides the ` `    ``// number, return true; ` `    ``return` `true``; ` `} ` ` `  `// Function to find the smallest ` `// number not divisible by any ` `// of its digits. ` `static` `void` `solve(``int` `n) ` `{ ` `     `  `    ``// Get the lower range of n ` `    ``// digit number ` `    ``int` `L = power(10, n - 1); ` `     `  `    ``// Get the high range of n ` `    ``// digit number ` `    ``int` `R = power(10, n) - 1; ` `     `  `    ``int` `flag = 0; ` `     `  `    ``// Check all the N-digit numbers ` `    ``for``(``int` `i = L; i <= R; i++)  ` `    ``{ ` `        ``bool` `answer = check(i); ` `         `  `        ``// If the first number to satify ` `        ``// the constraints is found ` `        ``// print it, set the flag value ` `        ``// and break out of the loop. ` `        ``if` `(answer == ``true``) ` `        ``{ ` `            ``Console.WriteLine(i); ` `            ``flag++; ` `            ``break``; ` `        ``} ` `    ``} ` `     `  `    ``// If no such digit found, ` `    ``// return -1 as per problem statement ` `    ``if` `(flag == 0) ` `    ``Console.WriteLine(``"-1"``); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `N = 4; ` `     `  `    ``solve(N); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```2227
```

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : offbeat, 29AjayKumar

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.